The Book: Third Edition

Summary

Effective Python Book Cover

Python is a versatile and powerful language, but leveraging its full potential requires more than just knowing the syntax. Effective Python: 125 Specific Ways to Write Better Python, 3rd Edition is your comprehensive guide to mastering Python’s unique strengths and avoiding its hidden pitfalls. This updated edition (published by Pearson Addison-Wesley in November, 2024) builds on the acclaimed second edition, expanding from 90 to 125 best practices that are essential for writing high-quality Python code.

Drawing on years of experience at Google, Brett Slatkin offers clear, concise, and practical advice for both new and experienced Python developers. Each item in the book provides insight into the “Pythonic” way of programming, helping you understand how to write code that is not only effective but also elegant and maintainable. Whether you’re building web applications, analyzing data, writing automation scripts, or training AI models, this book will equip you with the skills to make a significant impact using Python.

Buy the Book on AmazonBuy DRM-free eBook

Already Have the Book?

Visit the GitHub project to see all of the code snippets from the book in one place. Run and modify the example code yourself to confirm your understanding. You can also report any errors you’ve found.

For future updates about the book, related videos, translations, conference presentations, and more, choose one of these ways to stay in touch:

More Info

What This Book Covers

Each chapter in Effective Python contains a broad but related set of items. Each item contains concise and specific guidance explaining how you can write Python programs more effectively. Items include advice on what to do, what to avoid, how to strike the right balance, and why this is the best choice. Items reference each other to make it easier to fill in the gaps as you read. Feel free to jump between items and follow your interest.

This third edition covers the language up through Python version 3.13. This book includes 35 completely new items compared to the second edition, two new chapters focused on robustness and performance, and in-depth coverage of creating C-extension modules and interfacing with native shared libraries. Most of the items from the second edition have been revised and included, but many have undergone substantial updates. For some items, my advice has completely changed (compared to the second edition) due to best practices evolving as Python has matured.

Table of Contents

Chapter 1: Pythonic Thinking

  1. Know Which Version of Python You’re Using
  2. Follow the PEP 8 Style Guide
  3. Never Expect Python to Detect Errors at Compile Time
  4. Write Helper Functions Instead of Complex Expressions
  5. Prefer Multiple-Assignment Unpacking over Indexing
  6. Always Surround Single-Element Tuples with Parentheses
  7. Consider Conditional Expressions for Simple Inline Logic
  8. Prevent Repetition with Assignment Expressions
  9. Consider match for Destructuring in Flow Control; Avoid When if Statements Are Sufficient

Chapter 2: Strings and Slicing

  1. Know the Differences Between bytes and str
  2. Prefer Interpolated F-Strings over C-Style Format Strings and str.format
  3. Understand the Difference Between repr and str when Printing Objects
  4. Prefer Explicit String Concatenation over Implicit, Especially in Lists
  5. Know How to Slice Sequences
  6. Avoid Striding and Slicing in a Single Expression
  7. Prefer Catch-All Unpacking over Slicing

Chapter 3: Loops and Iterators

  1. Prefer enumerate over range
  2. Use zip to Process Iterators in Parallel
  3. Avoid else Blocks After for and while Loops
  4. Never Use for Loop Variables After the Loop Ends
  5. Be Defensive when Iterating over Arguments
  6. Never Modify Containers While Iterating over Them; Use Copies or Caches Instead
  7. Pass Iterators to any and all for Efficient Short-Circuiting Logic
  8. Consider itertools for Working with Iterators and Generators

Chapter 4: Dictionaries

  1. Be Cautious when Relying on Dictionary Insertion Ordering
  2. Prefer get over in and KeyError to Handle Missing Dictionary Keys
  3. Prefer defaultdict over setdefault to Handle Missing Items in Internal State
  4. Know How to Construct Key-Dependent Default Values with __missing__
  5. Compose Classes Instead of Deeply Nesting Dictionaries, Lists, and Tuples

Chapter 5: Functions

  1. Know That Function Arguments Can Be Mutated
  2. Return Dedicated Result Objects Instead of Requiring Function Callers to Unpack More Than Three Variables
  3. Prefer Raising Exceptions to Returning None
  4. Know How Closures Interact with Variable Scope and nonlocal
  5. Reduce Visual Noise with Variable Positional Arguments
  6. Provide Optional Behavior with Keyword Arguments
  7. Use None and Docstrings to Specify Dynamic Default Arguments
  8. Enforce Clarity with Keyword-Only and Positional-Only Arguments
  9. Define Function Decorators with functools.wraps
  10. Prefer functools.partial over lambda Expressions for Glue Functions

Chapter 6: Comprehensions and Generators

  1. Use Comprehensions Instead of map and filter
  2. Avoid More Than Two Control Subexpressions in Comprehensions
  3. Reduce Repetition in Comprehensions with Assignment Expressions
  4. Consider Generators Instead of Returning Lists
  5. Consider Generator Expressions for Large List Comprehensions
  6. Compose Multiple Generators with yield from
  7. Pass Iterators into Generators as Arguments Instead of Calling the send Method
  8. Manage Iterative State Transitions with a Class Instead of the Generator throw Method

Chapter 7: Classes and Interfaces

  1. Accept Functions Instead of Classes for Simple Interfaces
  2. Prefer Object-Oriented Polymorphism over Functions with isinstance Checks
  3. Consider functools.singledispatch for Functional-Style Programming Instead of Object-Oriented Polymorphism
  4. Prefer dataclasses for Defining Lightweight Classes
  5. Use @classmethod Polymorphism to Construct Objects Generically
  6. Initialize Parent Classes with super
  7. Consider Composing Functionality with Mix-in Classes
  8. Prefer Public Attributes over Private Ones
  9. Prefer dataclasses for Creating Immutable Objects
  10. Inherit from collections.abc Classes for Custom Container Types

Chapter 8: Metaclasses and Attributes

  1. Use Plain Attributes Instead of Setter and Getter Methods
  2. Consider @property Instead of Refactoring Attributes
  3. Use Descriptors for Reusable @property Methods
  4. Use __getattr__, __getattribute__, and __setattr__ for Lazy Attributes
  5. Validate Subclasses with __init_subclass__
  6. Register Class Existence with __init_subclass__
  7. Annotate Class Attributes with __set_name__
  8. Consider Class Body Definition Order to Establish Relationships Between Attributes
  9. Prefer Class Decorators over Metaclasses for Composable Class Extensions

Chapter 9: Concurrency and Parallelism

  1. Use subprocess to Manage Child Processes
  2. Use Threads for Blocking I/O; Avoid for Parallelism
  3. Use Lock to Prevent Data Races in Threads
  4. Use Queue to Coordinate Work Between Threads
  5. Know How to Recognize When Concurrency Is Necessary
  6. Avoid Creating New Thread Instances for On-demand Fan-out
  7. Understand How Using Queue for Concurrency Requires Refactoring
  8. Consider ThreadPoolExecutor When Threads Are Necessary for Concurrency
  9. Achieve Highly Concurrent I/O with Coroutines
  10. Know How to Port Threaded I/O to asyncio
  11. Mix Threads and Coroutines to Ease the Transition to asyncio
  12. Maximize Responsiveness of asyncio Event Loops with async-friendly Worker Threads
  13. Consider concurrent.futures for True Parallelism

Chapter 10: Robustness

  1. Take Advantage of Each Block in try/ except/ else/ finally
  2. assert Internal Assumptions and raise Missed Expectations
  3. Consider contextlib and with Statements for Reusable try/ finally Behavior
  4. Always Make try Blocks as Short as Possible
  5. Beware of Exception Variables Disappearing
  6. Beware of Catching the Exception Class
  7. Understand the Difference Between Exception and BaseException
  8. Use traceback for Enhanced Exception Reporting
  9. Consider Explicitly Chaining Exceptions to Clarify Tracebacks
  10. Always Pass Resources into Generators and Have Callers Clean Them Up Outside
  11. Never Set __debug__ to False
  12. Avoid exec and eval Unless You’re Building a Developer Tool

Chapter 11: Performance

  1. Profile Before Optimizing
  2. Optimize Performance-Critical Code Using timeit Microbenchmarks
  3. Know When and How to Replace Python with Another Programming Language
  4. Consider ctypes to Rapidly Integrate with Native Libraries
  5. Consider Extension Modules to Maximize Performance and Ergonomics
  6. Rely on Precompiled Bytecode and File System Caching to Improve Startup Time
  7. Lazy-Load Modules with Dynamic Imports to Reduce Startup Time
  8. Consider memoryview and bytearray for Zero-Copy Interactions with bytes

Chapter 12: Data Structures and Algorithms

  1. Sort by Complex Criteria Using the key Parameter
  2. Know the Difference Between sort and sorted
  3. Consider Searching Sorted Sequences with bisect
  4. Prefer deque for Producer-Consumer Queues
  5. Know How to Use heapq for Priority Queues
  6. Use datetime Instead of time for Local Clocks
  7. Use decimal When Precision Is Paramount
  8. Make pickle Serialization Maintainable with copyreg

Chapter 13: Testing and Debugging

  1. Verify Related Behaviors in TestCase Subclasses
  2. Prefer Integration Tests over Unit Tests
  3. Isolate Tests From Each Other with setUp, tearDown, setUpModule, and tearDownModule
  4. Use Mocks to Test Code with Complex Dependencies
  5. Encapsulate Dependencies to Facilitate Mocking and Testing
  6. Use assertAlmostEqual to Control Precision in Floating Point Tests
  7. Consider Interactive Debugging with pdb
  8. Use tracemalloc to Understand Memory Usage and Leaks

Chapter 14: Collaboration

  1. Know Where to Find Community-Built Modules
  2. Use Virtual Environments for Isolated and Reproducible Dependencies
  3. Write Docstrings for Every Function, Class, and Module
  4. Use Packages to Organize Modules and Provide Stable APIs
  5. Consider Module-Scoped Code to Configure Deployment Environments
  6. Define a Root Exception to Insulate Callers from APIs
  7. Know How to Break Circular Dependencies
  8. Consider warnings to Refactor and Migrate Usage
  9. Consider Static Analysis via typing to Obviate Bugs
  10. Prefer Open Source Projects for Bundling Python Programs over zipimport and zipapp

Previous editions

If, for whatever reason, you’re still primarily using Python 2, despite its end-of-life in April, 2020, the first edition of the book will be more useful to you. For older versions of Python 3, the second edition of this book might be useful.

About the Author

Brett Slatkin

Brett Slatkin has been programming with Python professionally for the past 19 years. He currently works as a principal software engineer in the Office of the CTO at Google, developing technology strategies and rapid prototypes.

His prior experience includes founding Google Surveys, an internal startup for collecting machine learning and market research data sets; launching Google App Engine, the company’s first cloud computing product; scaling Google’s A/B experimentation products to billions of users; co-creating PubSubHubbub, the W3C standard for real-time RSS feeds; and making various contributions to open source projects.

Brett earned a bachelor’s degree in computer engineering from Columbia University in the City of New York. Outside of his day job, he enjoys playing piano, surfing, and spending time with his family. He lives in California.

Updates

Preorder the Third Edition

Sat 07 September 2024

Effective Python: Third Edition is now available for preorder! Follow this link to buy your copy in advance. It will ship in late November 2024 once the book has finished printing and is stocked in the warehouse. Digital editions will become available when the physical book ships or sooner.