On this page
  1. this
  2. Import
  3. Overview
  4. Quick Start
  5. Implementation Walkthrough
  6. 1. Inspecting PEP 20 Programmatically
  7. Gotchas & Boundaries

this

Prev: textwrap | Up: stdlib_module | Next: threading
Rel: sys, sysconfig, builtins, warnings, dataclasses
Ref: docs.python.org — this
Tags: #public #python3 #stdlib


Import

import this

Overview

Python Easter egg module that prints The Zen of Python (PEP 20, by Tim Peters) to sys.stdout on import, storing the text internally as a ROT13-encoded string (this.s) decoded via the lookup dict this.d. A playful reminder of Python's design philosophy ("Explicit is better than implicit", "Simple is better than complex", "Readability counts").

Quick Start

import contextlib
import io

buf = io.StringIO()
with contextlib.redirect_stdout(buf):
    import this

# Accessing the ROT13 decoding table directly:
decoded = "".join(this.d.get(c, c) for c in this.s)
assert "Beautiful is better than ugly." in decoded

Implementation Walkthrough

1. Inspecting PEP 20 Programmatically

Because import this only prints on the first import (subsequent imports hit sys.modules["this"]), read this.s and this.d if you ever need the Zen of Python text in code.

Gotchas & Boundaries

Linked from

Related notes