On this page
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
- Import-time side effect: Importing
thiswrites 21 lines tosys.stdoutimmediately. - Intentionally convoluted code: The implementation of
this.pydeliberately violates several Zen of Python aphorisms as an in-joke.
Linked from
Related notes
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/textwrap Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/threading Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/builtins Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/dataclasses Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/sys Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/sysconfig Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/warnings Linked here; shared tags: #stdlib
- tuts/c/stdlib shared tags: #stdlib