Published on

Pythonic

Authors

I have been working with Python more and more. It is a very readable language and very easy to follow. As someone with experience in other languages I keep running into the term "pythonic". It basically means that there is a more Python way of doing something when working in the language.

This comes up when people from other languages try to do something from the language they are familiar with in Python. Now Python being a general purpose, multi paradigm language is more than capable of doing these things. But it was built with a certain ethos in mind.

When moving to a new language it is important to understand how things are done in that language. I have found that by becoming familiar with the new language's approach to doing something you often learn a new way of thinking and dealing with different problems.

For example after working with Go I really began to prefer the Go way of dealing with errors. The Go way of dealing with errors is to return an err object together with the value you asked a method for. Then before using the value you check if the error is defined, if it is you handle it otherwise you work with the value that was returned.

This is different to many other languages where you instead throw exceptions when there are errors. With this throw approach you are forced to wrap your code in try catch statements everywhere to deal with these errors which makes the code quite verbose.

After really taking a liking to this Go approach of dealing with errors I started using it in JavaScript too. I did this by returning a JSON object with the value I want and an error object which like Go I check. This approach makes my code much cleaner and also allows me to do things like make a general error handler function that I can pass errors to in order to check and handle errors. If I want to do something specific with certain errors I can handle that on an ad hoc basis in the code.

What I have learnt from working with many different languages is that it is better to leave the ways of thinking from the languages you are used to at the door and instead understand and get a feel for how things are done in the new language. This will improve you as a developer as you will develop different ways of coding and tackling problems.