Effective Python is Brett Slatkin's book of Python best practices, published by Addison-Wesley and organized as a catalog of short, self-contained Items, each a concrete rule with rationale and trade-offs. The Items cover Pythonic style, comprehensions and generators, function arguments, class design with properties, metaclasses, concurrency under the GIL, robustness and testing. The current 3rd edition of 20 November 2024 expands the 2nd edition's 90 Items to 125.
Effective Python
Details
Core Concepts:
- Items format
-
A catalog of ~125 short, self-contained "Items", each a concrete best-practice rule with rationale and trade-offs (grouped into chapters)
- Pythonic thinking
-
Follow PEP 8 style and the Zen of Python; prefer f-strings over
%andstr.format; know the difference betweenbytesandstr; prefer helper functions to complex expressions - Lists and dictionaries
-
Prefer comprehensions to
map/filter; useenumerateinstead ofrange; usezipto iterate in parallel; use catch-all unpacking over indexing; avoidelseblocks afterfor/while - Functions
-
Prefer raising exceptions to returning
None; enforce clarity with keyword-only and positional-only arguments; return generators instead of building lists; never modify default argument values (useNoneand docstrings) - Comprehensions and generators
-
Use generator expressions for large comprehensions; compose generators with
yield from; reach foritertoolsbefore hand-rolling iteration - Classes and interfaces
-
Compose classes rather than nesting dicts/tuples deeply; use
@classmethodpolymorphism; prefer public attributes and@propertyover getters/setters; use functions instead of classes for simple interfaces - Metaclasses and attributes
-
Use
@propertyand descriptors for reusable attribute behaviour; prefergetattr/getattributesparingly; validate subclasses withinit_subclass - Concurrency and parallelism
-
Use threads for blocking I/O, not parallelism (the GIL); use
Queueto coordinate pipelines; use coroutines andasynciofor many concurrent functions; useconcurrent.futuresand processes for true parallelism - Robustness, testing, and collaboration
-
Make programs robust and performant (new chapters in the 3rd edition); test with
unittest/Mock; document with docstrings; isolate dependencies with virtual environments (venv) - Key Proponent
-
Brett Slatkin ("Effective Python", Addison-Wesley) — principal software engineer at Google and creator of the book’s runnable example suite
When to Use:
-
Onboarding Python developers to idiomatic, robust ("Pythonic") code
-
Code review discussions grounded in a shared, citable rule set ("Item 30: consider generators instead of returning lists")
-
Establishing team-wide Python coding standards and conventions
-
Explaining Python-specific idioms and pitfalls (comprehensions, generators, the GIL,
@property, keyword-only arguments) -
Prompting an LLM to produce idiomatic, production-quality Python code
Related Anchors:
Current Status:
-
The 3rd edition (Pearson Addison-Wesley, published 20 November 2024, ISBN 978-0-13-817218-3) is the current edition. It expands from the 2nd edition’s 90 Items to 125 Items — 35 new Items plus two new chapters on robustness and performance
-
The 1st edition (2015) targeted Python 2 as well as 3; the 2nd edition (2019) was Python-3-only through ~3.8; the 3rd edition covers modern Python 3, including features such as the walrus operator, structural pattern matching, and updated typing and
asyncioguidance -
The book’s core advice (prefer comprehensions and generators, respect the GIL, compose classes, use
@property) remains stable across editions; only specific Items track the language. A training-data prior keyed on "Effective Python" most plausibly reflects the 2nd edition (90 Items) and is therefore silent on the 35 Items added in the 3rd edition