Featured image of post Python Easter Egg

Python Easter Egg

If you’re not familiar with easter eggs (not the real ones), they are messages that are hidden in things like movies, software, games, and other mediums. I won’t go into depth about them here but you can read the following links if you’d like to know more.

Easter egg (media) - Wikipedia

Eeggs

Onto Python though.

A well known Easter Egg in Python is the this library. If you import this into a Python shell or script then you will be greeted with the “Zen of Python” by Tim Peters. It says

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
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!

However, this isn’t what I wanted to point out in this post.

I’m relatively new to programming and I’m curious how about code works so I decided to look at the source code for the file. What I found confused me at first but then made me laugh once I understood what was going on. If you look at the source code for this.py, located in the lib directory of your Python installation, you will find this.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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]))

The variable s which contains the actual message is encoded in what’s known as ROT13. It’s a very simple cipher that was written in ancient Rome. It works by replacing each individual letter with the 13th letter after it in the alphabet.

This chunk of the code decodes the message

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

And the last line prints it to the users screen.

Basically, it breaks almost every guideline in the Zen of Python.

Very funny, Mr. Peters 😂

While this isn’t a secret by any means, it felt really good to find this little egg myself. While I didn’t initially understand what was going on, it also felt good finding the answer and getting the joke.

So poke around your favorite programs source code. Might just find something in there that will make you laugh.

Credit where it’s due: python - What is the source code of the "this" module doing? - Stack Overflow