Should I worry about making my code Pythonic ?
If you’re reading this, you are probably one of those programmers who is interested in knowing what Pythonic essentially means or want to understand why Pythonic ?
Idiomatic Code
In order to understand what Pythonic means, it’s important to realise what Idiomatic Code is. Idiomatic code is the type of code which closely resembles the features and ideals of a programming lanuguage. Such idiomatic code for Python is called Pythonic.
When you’re writing Pythonic code, you’re making your code
- More readable, efficient and maintainable for other Python developers who as well write pythonic code.
- Utilise best practices of code writing which has evolved over the years.
Once you follow a convention, as a programmer you free your brain from the relatively mundane aspects of programming, which offers little payback like naming classes, attributes or member functions. Moreover, you can focus more on the business logic or core part of your program.
Show me the code
Let’s say your boss asks you to write a Python code to swap two numbers. You can surely write it like this :
but a Pythonic way to do this would be this.
The above example is more on the lines of the features of the programming language. For more such examples, head over to this amazing GitHub repo.
Zen of Python
Align yourself with the Zen of Python. It defines the ideals of Python and more importantly it’s beautiful principles.
In the end, everything boils down to how efficiently you’re utilising your tools to be a best practioner. Writing Pythonic code is certainly the way to be a good Python programmer.