C.W.K.
Stream
Lesson 04 of 05 · published

The Zen of Python — and What It Actually Means

~15 min · zen, pep-20, philosophy, style

Level 0Curious
0 XP0/93 lessons0/23 achievements
0/100 XP to next level100 XP to go0% complete

The Zen — type 'import this' to read it

PEP 20, the Zen of Python, is 19 aphorisms by Tim Peters that summarize Python's design philosophy. import this in any Python interpreter prints them. Some are obvious ("Beautiful is better than ugly"), some are deep ("In the face of ambiguity, refuse the temptation to guess"), and a few are guidelines that decide actual API design choices.

The four that matter most

"There should be one — and preferably only one — obvious way to do it." When designing an API, resist providing five ways to do the same thing. Pick one good way. "Explicit is better than implicit." Make behavior visible at the call site; avoid magic. "Errors should never pass silently." Don't swallow exceptions; let them propagate or handle them deliberately. "If the implementation is hard to explain, it's a bad idea." If you can't describe what your code does, it's probably wrong.

The ones beginners misread

"Simple is better than complex" doesn't mean "always pick the simplest thing." It means "simple where simple works." The next line — "Complex is better than complicated" — is the corrective: when complexity is necessary, structure it (complex) rather than tangling it (complicated). Complex code with clear structure beats simple code that doesn't fit the problem.

The one that won't go away

"Now is better than never. Although never is often better than *right* now." Reads like a riddle. Means: ship something rather than wait for perfection — but don't ship something half-built that you'll regret. Discipline both ways.

Pythonic Way: Read the Zen at least once a year. The aphorisms are short; their applications are not. A senior Python developer's intuitions on API design, error handling, and naming choices reflect years of running code through these filters.

Code

Read the Zen yourself·python
import this

# 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!
Explicit > implicit — applied·python
# IMPLICIT — caller can't tell what mode this is in
result = process_data(items)

# EXPLICIT — call site shows the choice
result = process_data(items, mode="strict", retry=3)

# IMPLICIT — magic argument modification
def bad(items):
    items.sort()        # mutates the caller's list! surprising.

# EXPLICIT — return a sorted copy
def good(items):
    return sorted(items)
Errors should never pass silently — applied·python
# DON'T — silent failure
try:
    risky_operation()
except Exception:
    pass            # bug-hider supreme

# DO — handle the specific error and log
try:
    risky_operation()
except ValueError as e:
    logger.warning("value error: %s", e)
    # ... fallback or re-raise

# DO — let unexpected errors propagate
try:
    risky_operation()
except ValueError:
    handle_known_case()
# Other exceptions go up the stack — that's correct
One obvious way — applied·python
# UNPYTHONIC — too many ways to do the same thing
class MyList:
    def append(self, x): ...
    def add(self, x): ...           # do these two do the same thing?
    def push(self, x): ...          # what about this?
    def insert_at_end(self, x): ...
    def __iadd__(self, x): ...

# PYTHONIC — pick one canonical name
class MyList:
    def append(self, x): ...        # the One Way

External links

Exercise

Pick three of the Zen aphorisms that you find most actionable. For each, write a one-sentence example of what it does NOT mean (a common misreading). Then write a one-sentence example of code that violates it and a corrected version. The point isn't to memorize the Zen; it's to internalize what each line is steering away from.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.