Home Projects Philosophy Hobbies About Me Updates

Zen of Python Considered Harmful

When the Way is forgotten
Duty and justice appear;
Then knowledge and wisdom are born
Along with hypocrisy.
— Lao Tze, Tao De Jing

How to have lots of ideas?

Brainstorming through a lot of different and varied ideas is not easy. We tend to stick to the first idea we had, polish it, turn it in our heads, and there is no longer room for the further ideas that we might have. There is however a simple trick that you can use to get an idea out of your head and forget it easily:

Write it down.

There is something magical in the act of writing an idea down. It kills it, freeing us to move on.

The what of Python?

"The Zen of Python" is a short list of rules, which was posted by Tim Peters to the Python's mailing list, humorously summarizing the unspoken set of rules of thumb that were used throughout the development of the Python language to make decisions about its shape. Unfortunately, not everyone got the joke. Soon the rules were included in the Python's standard library as a module confusingly called "this", and they were on their way to becoming the dogma and destroying the language we love. Written down, the ideas lost their power, and started be used to win arguments. Their Zen-like nature made them perfect for that, as we will soon see.

Obey

As son as the "Zen of Python" was enshrined, the Python core developers started changing, and by the Python 3 disaster there was practically no shred of the old philosophy left. Python has been rebuilt anew, ignoring practically all the heuristics that originally made it so great. Here are some examples.

Beautiful is better than ugly.

I think I will skip the examples for this one. It would just be too easy and too cruel.

Explicit is better than implicit.

Remember how Python got around the problem of multiple inheritance?

class B(A):
    def __init__(self):
        super(B, self).__init__()

But that was too explicit. So now you can do:

class B(A):
    def __init__(self):
        super().__init__()

Flat is better than nested.

That's why the "with" statement is now used for practically everything!

Readability counts.

Try to use the type annotations. I dare you.

Errors should never pass silently.

The os.listdir() will silently ignore and skip any filenames that it couldn't convert to Unicode.

Special cases aren't special enough to break the rules

Ever tried to raise an exception inside a finally block in an iterator generator called from a __del__ method?

There should be one — and preferably only one — obvious way to do it.

So how do you format text in Python? Oh, easy, there is the % operator for that!

hello = "world"
print "Hello %s!" % hello)

but why limit yourself? In one of the later versions of Python 2, the "format" method has been introduced:

hello = "world"
print "Hello {}!".format(hello))

and then, when that proved to not be confusing enough, we got the f-strings:

hello = "world"
print(f"Hello {hello}!")

Namespaces are one honking great idea — let's do more of those!

Since "Zen of Python" has been published, no new namespace has been introduced in Python. Even in such an obvious case as the type annotations, which beg for a separate namespace – after all, we are not going to be using the types in the code – we are reduced into putting from typing import * in every single file, littering our global namespace. Honking great idea, indeed.

So What is it Good For?

As we can see, the "Zen of Python" is no longer used for the development of Python itself, and, because the language itself deviates from it, it's also less and less useful for developing with Python. So what is it good for? Winning arguments!

But why is it called "zen"? It was called like that, because it is similar in its structure to many religious texts. What makes it similar? If you look closely, you will notice that every second line negates the line before it. That is a very important feature, because it lets you prove anything!

P∧¬P → Q

The above logic formula is an example of what we call a tautology. It is always true, no matter what the values of P and Q are. So, if we use two lines of the "Zen of Python" as P and ¬P, and whatever we want to prove as Q, we are always right! Yay!