Codementor Events

The Zen Of Python Is A Joke And Here Is Why

Published Mar 07, 2020Last updated Mar 09, 2020

The Zen of Python inspires programmers all over the world, but, it is not to be taken too literally.

The Venerable Zen, A Presentation

Typing this

import this

gives us

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!

that is called the Zen of python

The One Line That Is A Hilariously Good Joke

let us take a look at the following line.

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

most developers understand it to mean that python makes you write your code in one way, instead of having many ways to express it. many then go on to contrast the linguist-invented perl with the mathematician-invented python.

however, there are two catch with this line

  • should there be or should there preferably be
  • the --

so, should there be one way meaning, is it obligatory to have one way or is it recommended to have one way? is it compulsory or just recommended?

the line does not leave us to guesses, it actually showed it with the --.

to preserve consistency, it should have been either

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

or

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

but it mixes both

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

As David Goodger mentionned, see the last line:

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

Another way of using!

Saying Hi To The Import Itself

Let us take this line

Explicit is better than implicit.

but we import by

import this

oxforddictionaries.com defines explicit as

Stated clearly and in detail, leaving no room for confusion or doubt.

and implicit as

Suggested though not directly expressed.

so, was the name and way of printing explicit or implicit?

What The File Tell Us

Here is the zen module source code in py3.7

s = """Gur Mra bs Clguba, ol Gvz Crgref Ornhgvshy vf orggre guna htyl.
Rkcyvpvg vf orggre guna vzcyvpvg.
Fvzcyr vf orggre guna pbzcyrk.
Pbzcyrk vf orggre guna pbzcyvpngrq.
Syng vf orggre guna arfgrq.
Fcnefr vf orggre guna qrafr.
Ernqnovyvgl pbhagf.
Fcrpvny pnfrf nera'g fcrpvny rabhtu gb oernx gur ehyrf.
Nygubhtu cenpgvpnyvgl orngf chevgl.
Reebef fubhyq arire cnff fvyragyl.
Hayrff rkcyvpvgyl fvyraprq.
Va gur snpr bs nzovthvgl, ershfr gur grzcgngvba gb thrff.
Gurer fubhyq or bar-- naq cersrenoyl bayl bar --boivbhf jnl gb qb vg.
Nygubhtu gung jnl znl abg or boivbhf ng svefg hayrff lbh'er Qhgpu.
Abj vf orggre guna arire.
Nygubhtu arire vf bsgra orggre guna *evtug* abj.
Vs gur vzcyrzragngvba vf uneq gb rkcynva, vg'f n onq vqrn.
Vs gur vzcyrzragngvba vf rnfl gb rkcynva, vg znl or n tbbq vqrn.
Anzrfcnprf ner bar ubaxvat terng vqrn -- yrg'f qb zber bs gubfr!""" 

d = {}
for c in (65, 97):
    for i in range(26):
        d[chr(i+c)] = chr((i+13) % 26 + c)

print("".join([d.get(c, c) for c in s]))

and yet, in the zen we see

Readability counts.

very readable ...

But Wait, There Are Exceptions

Well, you are right, there are exceptions. yes maybe the zen file is an exception to the zen but wait ... the zen tells us

Special cases aren't special enough to break the rules.

ok then ... guess we've got the message.

Conclusion

We should not take it too seriously.

Discover and read more posts from Abdur-Rahmaan Janhangeer
get started
post comments2Replies
David Goodger
5 years ago

There are actually 3 ways that the dashes are used. Look at the last line!

Abdur-Rahmaan Janhangeer
5 years ago

Thanks for commenting. Added a reference to you in the article. Btw this article by is featured in many places such as awesome zen on github