Pythonic == easier to read / maintain
In video 6: “Problem solving using more than one solution method” of Intermediate Python programming I stumbled upon a great example of less vs. more Pythonic code.
I like to post this example here to demonstrate that Pythonic code is so much easier to read and maintain (not to mention the satisfaction of writing it!).
Although I was already familiar with reversing a list by using -1 as stride, it was a great reminder that language idioms matter. It makes your code more concise. It leads to more readable code. And more readable code equals more maintainable code!
I posted the example (with explanation and extension) in this notebook.
This is where the training video ended. I made two additions:
- A. use max with key=len to get the longest palindrome, and
- B. wrap the two pieces of functionalities in two methods.
One note on naming. Uncle Bob has a great video on naming. One subtle trick is to name boolean methods as is_, so is_palindrome makes you expect a boolean being returned. Which is useful as Python (unlike Java for example) does not force you to specify the return type. get_longest_palindrome clearly expresses the intent to retrieve one value, the longest palindrome. Naming is communication!
The initial code cannot be considered Pythonic, the final code can be in my opinion.
We achieved a couple of things:
- the code is shorter (from 8 to 4 lines)
- the code is more readable and maintainable (using methods that express their intent)
- we can be at pease: we have used two neat language features, making the code more concise
- last but not least, it feels great writing Pythonic code :)
If you liked this post consider joining my new Python Facebook group to learn some more Python together …