Cover of Effective Python by Brett Slatkin, featuring abstract geometric shapes representing structured, layered code patterns on a dark background

Pages

1262

Published

2019

Effective Python

90 Specific Ways to Write Better Python

Master the idioms, patterns, and best practices that separate working Python code from production-quality Python code.

Effective Python gives you 90 concrete, independent items β€” each a specific technique, pattern, or pitfall β€” that make your Python code cleaner, faster, and easier to maintain. Written by Brett Slatkin, a senior engineer at Google, the book covers everything from generator expressions and concurrency primitives to metaclasses and memory management. Each item explains not just what to do, but why it works and what goes wrong when you ignore it.

About this book

Most Python developers learn the language by getting things to work. They pick up syntax, discover the standard library, and ship code that runs. But there is a significant gap between code that runs and code that a team can maintain, extend, and trust in production. Effective Python closes that gap.

Brett Slatkin organizes the book into 90 discrete, self-contained items. Each one targets a real decision you face when writing Python: which sequence type to reach for, when to use a generator instead of a list comprehension, how to write a class hierarchy that does not fight the language, and dozens more. You can read straight through or jump to the item you need today.

The book is organized into thematic sections that mirror how real programs grow in complexity:

  • Pythonic thinking β€” writing code that reads as Python, not as C or Java translated into Python syntax
  • Lists and dictionaries β€” the data structures you use every day, used correctly
  • Functions β€” closures, default arguments, keyword-only parameters, and where each belongs
  • Comprehensions and generators β€” producing sequences lazily without sacrificing readability
  • Classes and interfaces β€” inheritance, mixins, descriptors, and when to use each
  • Metaclasses and attributes β€” the machinery behind frameworks, ORMs, and introspection-heavy libraries
  • Concurrency and parallelism β€” threads, processes, coroutines, and the asyncio event loop
  • Robustness and performance β€” testing, profiling, memory usage, and the tools that surface regressions early

Each item follows the same structure: a clear statement of the practice, a realistic code example showing the problematic pattern, the corrected version, and an explanation of the trade-offs. Nothing is hand-waved. If a technique has a cost, Slatkin tells you.

This is not a reference manual. It does not document every parameter of every function in the standard library. It is an opinionated, experience-backed guide to the decisions that separate beginner Python from the kind of Python that holds up under real pressure. If you already know the basics and want to write code that more senior engineers respect, this is the book to read next.

🎯 What you'll learn

  • Apply Pythonic idioms consistently so your code is immediately readable to other Python practitioners
  • Choose the right sequence, mapping, and set types for each situation and understand the performance consequences of each choice
  • Use generators, comprehensions, and itertools to process large or infinite sequences without loading everything into memory
  • Design class hierarchies that leverage Python's multiple inheritance, mixins, and descriptor protocol correctly
  • Write concurrent programs using threads, multiprocessing, and asyncio, knowing when each model fits the problem
  • Profile and optimize Python code using the standard library's built-in tools to find real bottlenecks rather than guessing
  • Structure packages, modules, and virtual environments so that large codebases stay navigable and dependency conflicts stay rare
  • Test your code with unittest and built-in debugging tools in ways that catch regressions before they reach production

πŸ‘€ Who is this book for?

  • Intermediate Python developers who know the syntax but want to write code that is genuinely idiomatic
  • Engineers migrating from Java, C++, or another language who need to shed habits that do not translate well to Python
  • Data scientists and analysts who write Python daily and want their code to be maintainable beyond a single notebook
  • Backend engineers building services or APIs in Python who need to understand concurrency and robustness patterns
  • Senior developers preparing for Python-focused code reviews who want a precise vocabulary for what makes code good or bad

Table of contents

  1. 01

    Pythonic Thinking

    Learn the core style rules and mental model that define idiomatic Python, including how to use the Python version system, follow PEP 8 conventions, and prefer Python-native constructs over patterns borrowed from other languages.

  2. 02

    Lists and Dictionaries

    Work with Python's most-used data structures correctly, covering slicing, starred unpacking, sorting, dictionary ordering guarantees, and when to use defaultdict or Counter instead of rolling your own logic.

  3. 03

    Functions

    Master the nuances of Python functions, including how closures capture variables, how default argument values are evaluated, and how keyword-only and positional-only parameters make call sites clearer and safer.

  4. 04

    Comprehensions and Generators

    Replace verbose loops with comprehensions and generator expressions, learning when each form is appropriate and how to use itertools to compose lazy pipelines that handle arbitrarily large data sets.

  5. 05

    Classes and Interfaces

    Build class hierarchies that feel natural in Python, covering public and private attributes, the difference between inheritance and composition, how to use @property, and when abstract base classes add value.

  6. 06

    Metaclasses and Attributes

    Understand how Python constructs classes at runtime and use metaclasses, __init_subclass__, and descriptors to write the kind of introspective machinery that underpins frameworks and ORMs.

  7. 07

    Concurrency and Parallelism

    Navigate Python's concurrency options, from threads and the GIL through multiprocessing and subprocess to native coroutines and the asyncio event loop, with guidance on which model fits which class of problem.

  8. 08

    Robustness and Performance

    Apply the practical engineering practices that keep production Python healthy: defensive exception handling, profiling with cProfile, memory measurement, and writing tests that catch regressions automatically.

  9. 09

    Testing and Debugging

    Use unittest, mocks, and Python's built-in debugging and introspection tools to write test suites that are fast, deterministic, and actually informative when they fail.

  10. 10

    Collaboration and Packaging

    Organize large Python codebases into well-structured packages, manage dependencies with virtual environments, and use type annotations and documentation conventions that help teammates contribute without introducing subtle bugs.

Frequently asked questions

Do I need to be an expert Python programmer to get value from this book?

You should already be comfortable writing basic Python, including functions, classes, and standard library usage. The book is aimed at developers who know how Python works and want to use it more effectively, not at absolute beginners.

Is this book specific to Python 3?

Yes. The third edition covers Python 3 throughout and does not maintain Python 2 compatibility. Specific items call out version differences where behavior changed meaningfully between Python 3.x releases.

Does the book need to be read cover to cover?

No. Each of the 90 items is self-contained with its own code examples and explanation. You can read sequentially or jump directly to the items most relevant to what you are working on today.

Is this book useful for data science work, or is it focused on software engineering?

The practices apply to any serious Python work. Data scientists who write reusable libraries, pipelines, or shared notebooks will find the sections on functions, classes, generators, and testing directly applicable.

Does the book cover async and concurrency?

Yes, there is a dedicated section on concurrency and parallelism covering threads, multiprocessing, and the asyncio event loop with concrete guidance on when to use each approach.

At over 1,200 pages, is this book dense or approachable?

The length reflects 90 complete, standalone items rather than a single long narrative. Most items run 10 to 20 pages and can be read in a single sitting, making the book more approachable than its page count suggests.

You might also like

πŸ“¬ Weekly Newsletter

Stay ahead of the curve

Get the best programming tutorials, data analytics tips, and tool reviews delivered to your inbox every week.

No spam. Unsubscribe anytime.