What is the source code of the this module doing?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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
Output:
The Source Code
The actual source code of this.py is remarkably short:
How the ROT13 Cipher Works
The translation process uses ROT13 (rotate by 13), a simple substitution cipher:
- 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:
- Decode the string: Each character in
sis looked up in the dictionary. Non-alphabetic characters (spaces, punctuation, newlines) pass through unchanged viad.get(c, c):
ROT13 is its own inverse — applying it twice returns the original text:
Accessing the Internals
Python's Other Easter Eggs
The this module is one of several Easter eggs in Python:
Using ROT13 in Python
The this module's cipher is also available as a standard codec:
Common Pitfalls
- Side effect on import:
import thisprints 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.dvscodecs: Thethis.ddictionary only handles ASCII letters. For a complete ROT13 implementation, usecodecs.encode(text, 'rot_13').
Summary
- Python's
thismodule 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.sand the cipher dictionary viathis.d
Related reading
- What is the standard way to add N seconds to datetime.time?
- What is the syntax to insert one list into another list in python?
- What is the threshold for the sklearn roc_auc_score
- What is the time complexity of heapq.nlargest?
- What is the time complexity of popping an element from a dict in Python?
- What is the use case for pip install -e?
- What is the use of "assert" in Python?
- What is the use of assert in Python?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.