Codementor Events

The Zen of Python

Published Feb 09, 2019Last updated Aug 07, 2019

Earlier tonight, a student of mine and I ran across a unique syntax:

def insertion_wrapper(f):
  # code goes here...


class LinkedList:
# some code for LinkedList class
# ...
  @_insertion_wrapper
  def insertion_sort(self):
    # code goes here...

And, in searching for help, I began reading from one of those PEPs...

I forget which it was, no matter.

I was checking out the index and came across The Zen of Python.

I thought this was interesting.

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!

Indeed, a beautiful post.

At the bottom of PEP 20 there is an "Easter Egg"...they tell us to import this...

Doing so in a python3 terminal produces the above text.

Me being the enterprising, exploring hacker that I want to be, did the following:

$ python3
> import this
...
> this
<module 'this' from '/usr/lib/python3.6/this.py'>

INTERESTING!

Let us look at the contents of this file:

cat /usr/lib/python3.6/this.py                             
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]))

So, there is some sort of basic cipher going on here.

Let us break it down...this is my first time looking at this:

for c in (65, 97):

65 is capital 'A' and 97 is lowercase 'a'.

for i in range(26):

26 letters in the alphabet.

d[chr(i+c)] = chr((i+13) % 26 + c)

d is a dictionary.

The key into d is going to be chr(i+c). i is iterating from 0 to 26. Each entry into d is each alphabetic character in both upper and lowercase.

What about the value?

chr((i+13) % 26 + c)

Whoa, what's going on here?

So, the letter i gets shifted up/forward/ahead by 13, then we mod that by 26 to give us yet another letter from A to Z, then we add the offset c.

d becomes a map on how to convert each character, effectively.

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

It is snippets like this why I really love Python.

List comprehensions are great.

For each character c in s, we are grabbing the corresponding character from d.

It is a simple cipher, and kind of funny that this is built into Python.

Discover and read more posts from Mike Bell
get started
post commentsBe the first to share your opinion
Amelia_S
5 years ago

Hi,
This is a nice post ! I enjoyed reading it

digitaocean siteground ipage

Johannes Riecken
5 years ago

I guess that’s a nice post for its target audience, but from the excerpt in the newsletter I expected it to be about the initial code or to expand on the Zen of Python itself, so maybe you could optimize your future posts so that the heading and first sentences give readers a better idea what to expect :)

Mike Bell
5 years ago

What except in what newsletter? I noted in the description that this was a blog post about PEP 20…How much more optimized could my summary be without you having to read the post? :)

Rob
5 years ago

Same reaction - this was at the top of the newsletter and the summary included was just the first few lines of the post (somewhat cryptic out of context). I clicked expecting something philosophical, not a dissection of the ‘this’ source. Nothing wrong with your article, Mike - just a misleading link.

Richard Robinson
5 years ago

The Zen of Perl, by bellaire

Beauty is subjective.
Explicit is recommended, but not required.
Simple is good, but complex can be good too.
And although complicated is bad,
Verbose and complicated is worse.
Brief is better than long-winded.
But readability counts.
So use whitespace to enhance readability.
Not because you’re required to.
Practicality always beats purity.
In the face of ambiguity, do what I mean.
There’s more than one way to do it.
Although that might not be obvious unless you’re a Monk.
At your discretion is better than not at all.
Although your discretion should be used judiciously.
Just because the code looks clean doesn’t mean it is good.
Just because the code looks messy doesn’t mean it is bad.
Reuse via CPAN is one honking great idea – let’s do more of that!

Show more replies