Python
this module
source code analysis
programming
code explanation

What is the source code of the this module doing?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Python's this module is an Easter egg that displays "The Zen of Python" — 19 guiding principles for writing Pythonic code, authored by Tim Peters. What makes the module interesting is not just the content but how it is implemented: the principles are encoded using a simple ROT13 cipher, making the source code itself a playful puzzle.

Running the Module

python
import this

Output:

 
1The Zen of Python, by Tim Peters
2
3Beautiful is better than ugly.
4Explicit is better than implicit.
5Simple is better than complex.
6Complex is better than complicated.
7Flat is better than nested.
8Sparse is better than dense.
9Readability counts.
10Special cases aren't special enough to break the rules.
11Although practicality beats purity.
12Errors should never pass silently.
13Unless explicitly silenced.
14In the face of ambiguity, refuse the temptation to guess.
15There should be one-- and preferably only one --obvious way to do it.
16Although that way may not be obvious at first unless you're Dutch.
17Now is better than never.
18Although never is often better than *right* now.
19If the implementation is hard to explain, it's a bad idea.
20If the implementation is easy to explain, it may be a good idea.
21Namespaces are one of the honking great ideas -- let's do more of those!

The Source Code

The actual source code of this.py is remarkably short:

python
1s = """Gur Mra bs Clguba, ol Gvz Crgref
2
3Ornhgvshy vf orggre guna htyl.
4Rkcyvpvg vf orggre guna vzcyvpvg.
5Fvzcyr vf orggre guna pbzcyrk.
6Pbzcyrk vf orggre guna pbzcyvpngrq.
7Syng vf orggre guna arfgrq.
8Fcnefr vf orggre guna qrafr.
9Ernqnovyvgl pbhagf.
10Fcrpvny pnfrf nera'g fcrpvny rabhtu gb oernx gur ehyrf.
11Nygubhtu cenpgvpnyvgl orngf chevgl.
12Reebef fubhyq arire cnff fvyragyl.
13Hayrff rkcyvpvgyl fvyraprq.
14Va gur snpr bs nzovthvgl, ershfr gur grzcgngvba gb thrff.
15Gurer fubhyq or bar-- naq cersrenoyl bayl bar --boivbhf jnl gb qb vg.
16Nygubhtu gung jnl znl abg or boivbhf ng svefg hayrff lbh'er Qhgpu.
17Abj vf orggre guna arire.
18Nygubhtu arire vf bsgra orggre guna *evtug* abj.
19Vs gur vzcyrzragngvba vf uneq gb rkcynva, vg'f n onq vqrn.
20Vs gur vzcyrzragngvba vf rnfl gb rkcynva, vg znl or n tbbq vqrn.
21Anzrfcnprf ner bar bs gur ubaxvat terng vqrnf -- yrg'f qb zber bs gubfr!"""
22
23d = {}
24for c in (65, 97):
25    for i in range(26):
26        d[chr(i+c)] = chr((i+13) % 26 + c)
27
28print("".join([d.get(c, c) for c in s]))

How the ROT13 Cipher Works

The translation process uses ROT13 (rotate by 13), a simple substitution cipher:

  1. Build the translation dictionary: The nested loops create mappings for all uppercase (starting at ASCII 65 = 'A') and lowercase (starting at ASCII 97 = 'a') letters. Each letter maps to the letter 13 positions ahead, wrapping around:
python
1d = {}
2for c in (65, 97):        # 65='A', 97='a'
3    for i in range(26):
4        d[chr(i+c)] = chr((i+13) % 26 + c)
5
6# Result:
7# 'A' -> 'N', 'B' -> 'O', ..., 'N' -> 'A', ...
8# 'a' -> 'n', 'b' -> 'o', ..., 'n' -> 'a', ...
  1. Decode the string: Each character in s is looked up in the dictionary. Non-alphabetic characters (spaces, punctuation, newlines) pass through unchanged via d.get(c, c):
python
"".join([d.get(c, c) for c in s])

ROT13 is its own inverse — applying it twice returns the original text:

python
1# Encoding
2"Hello" -> "Uryyb"
3# Decoding (same operation)
4"Uryyb" -> "Hello"

Accessing the Internals

python
1import this
2
3# The encoded string
4print(this.s[:50])  # "Gur Mra bs Clguba, ol Gvz Crgref\n\nOrnhgvshy vf o"
5
6# The translation dictionary
7print(this.d['A'])  # 'N'
8print(this.d['z'])  # 'm'
9
10# Manual decoding
11encoded = "Uryyb Jbeyq"
12decoded = "".join(this.d.get(c, c) for c in encoded)
13print(decoded)  # "Hello World"

Python's Other Easter Eggs

The this module is one of several Easter eggs in Python:

python
1# The Zen of Python
2import this
3
4# Antigravity (opens xkcd comic)
5import antigravity
6
7# Hello world from __hello__
8import __hello__  # prints "Hello world!"
9
10# Braces debate
11from __future__ import braces  # SyntaxError: not a chance

Using ROT13 in Python

The this module's cipher is also available as a standard codec:

python
1import codecs
2
3# Encode
4encoded = codecs.encode("Hello World", 'rot_13')
5print(encoded)  # "Uryyb Jbeyq"
6
7# Decode (same operation)
8decoded = codecs.encode(encoded, 'rot_13')
9print(decoded)  # "Hello World"

Common Pitfalls

  • Side effect on import: import this prints the Zen of Python immediately as a side effect. There is no way to import the module without printing (unless you redirect stdout).
  • Not for real encryption: ROT13 is a toy cipher with zero security. It was historically used on Usenet to hide spoilers, not to protect data.
  • The 20th principle: The Zen of Python has 19 aphorisms, but Tim Peters said there should be 20 — the last one was left for Guido van Rossum to fill in and never was.
  • this.d vs codecs: The this.d dictionary only handles ASCII letters. For a complete ROT13 implementation, use codecs.encode(text, 'rot_13').

Summary

  • Python's this module encodes "The Zen of Python" using ROT13 cipher
  • The source code builds a character substitution dictionary mapping each letter 13 positions forward
  • ROT13 is self-inverse — applying it twice returns the original text
  • The module prints its content as a side effect on import
  • Access the encoded text via this.s and the cipher dictionary via this.d

Course illustration
Course illustration

All Rights Reserved.