The "Zen of Python" is the set of guidelines that show up in your screen if you import this. If you have never read them before, read them now and again from time to time. If you are looking to write Pythonic code, write code that abides by the Zen of Python.

Want to improve your Python 🐍 problem-solving skills? The Python Problem-Solving Bootcamp is just starting! Join now!

A Python code snippet importing the module `this`.

To kick-off the Pydon't series we start with a set of guidelines that all Pythonistas should be aware of: the Zen of Python.

The Zen of Python is like a meta style guide. While you have things like PEP 8 that tell you how you should format your code, how to name your variables, etc., the Zen of Python provides you with the guidelines that you should follow when thinking about (Python) code and when designing a program.

Zen of Python

You can read the Zen of Python by executing import this in your REPL, which should print the following text:

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

Take a look at those guidelines and try to appreciate their meaning. If you want to write truly Pythonic code, then you should try to embrace these guidelines as much as possible.

Digging in the reference of PEP 20 -- The Zen of Python shows that Tim Peters (a major contributor to Python in its earlier days) thinks that these guidelines are β€œfundamental idiomatic recommendations for operating within the spirit of the [Python] language”, which goes to show that these recommendations are serious and should not be taken lightly - if you are willing to go the extra mile.

If you've seen the Kung Fu Panda, think of it this way: the Zen of Python is to Python programmers what the Dragon Scroll is to kung fu practitioners: Po was only able to take his kung fu skills to the next level, becoming truly amazing, after embracing the Dragon Scroll. You will only become a true Pythonista after you embrace the Zen of Python.

My advice would be to read this from time to time, and to try and remember the Zen of Python while you code and while you go over code that has already been written (by you or someone else). I don't know about you, but whenever I write a (text) document, like a letter or a blog post, I never get it right on the first try. I usually write a first draft and then go over it, editing as I see fit: sometimes reworking whole sections. Writing code is the same: chances are, the first thing you write can be greatly improved upon.

This Pydon't was more of a β€œmeta” Pydon't, with subjective advice on how to code. This might seem useless to you at first, but the more you dwell on it the more helpful it will become. The next Pydon'ts will show you objective, practical tips on how to write more Pythonic code.

If you liked this Pydon't be sure to leave a reaction below and share this with your friends and fellow Pythonistas.

Also, don't forget to subscribe to the newsletter so you don't miss a single Pydon't!


Want to improve your Python 🐍 problem-solving skills? The Python Problem-Solving Bootcamp is just starting! Join now!

Previous Post Next Post

Blog Comments powered by Disqus.