GeezerVison is showing effectivepython.com — Effective Python › The Book: Third Edition
Summary
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:
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
- Know Which Version of Python You’re Using
- Follow the PEP 8 Style Guide
- Never Expect Python to Detect Errors at Compile Time
- Write Helper Functions Instead of Complex Expressions
- Prefer Multiple-Assignment Unpacking over Indexing
- Always Surround Single-Element Tuples with Parentheses
- Consider Conditional Expressions for Simple Inline Logic
- Prevent Repetition with Assignment Expressions
- Consider
match for Destructuring in Flow Control; Avoid When if Statements Are Sufficient
Chapter 2: Strings and Slicing
- Know the Differences Between
bytes and str
- Prefer Interpolated F-Strings over C-Style Format Strings and
str.format
- Understand the Difference Between
repr and str when Printing Objects
- Prefer Explicit String Concatenation over Implicit, Especially in Lists
- Know How to Slice Sequences
- Avoid Striding and Slicing in a Single Expression
- Prefer Catch-All Unpacking over Slicing
Chapter 3: Loops and Iterators
- Prefer
enumerate over range
- Use
zip to Process Iterators in Parallel
- Avoid
else Blocks After for and while Loops
- Never Use
for Loop Variables After the Loop Ends
- Be Defensive when Iterating over Arguments
- Never Modify Containers While Iterating over Them; Use Copies or Caches Instead
- Pass Iterators to
any and all for Efficient Short-Circuiting Logic
- Consider
itertools for Working with Iterators and Generators
Chapter 4: Dictionaries
- Be Cautious when Relying on Dictionary Insertion Ordering
- Prefer
get over in and KeyError to Handle Missing Dictionary Keys
- Prefer
defaultdict over setdefault to Handle Missing Items in Internal State
- Know How to Construct Key-Dependent Default Values with
__missing__
- Compose Classes Instead of Deeply Nesting Dictionaries, Lists, and Tuples
Chapter 5: Functions
- Know That Function Arguments Can Be Mutated
- Return Dedicated Result Objects Instead of Requiring Function Callers to Unpack More Than Three Variables
- Prefer Raising Exceptions to Returning
None
- Know How Closures Interact with Variable Scope and
nonlocal
- Reduce Visual Noise with Variable Positional Arguments
- Provide Optional Behavior with Keyword Arguments
- Use
None and Docstrings to Specify Dynamic Default Arguments
- Enforce Clarity with Keyword-Only and Positional-Only Arguments
- Define Function Decorators with
functools.wraps
- Prefer
functools.partial over lambda Expressions for Glue Functions
Chapter 6: Comprehensions and Generators
- Use Comprehensions Instead of
map and filter
- Avoid More Than Two Control Subexpressions in Comprehensions
- Reduce Repetition in Comprehensions with Assignment Expressions
- Consider Generators Instead of Returning Lists
- Consider Generator Expressions for Large List Comprehensions
- Compose Multiple Generators with
yield from
- Pass Iterators into Generators as Arguments Instead of Calling the
send Method
- Manage Iterative State Transitions with a Class Instead of the Generator
throw Method
Chapter 7: Classes and Interfaces
- Accept Functions Instead of Classes for Simple Interfaces
- Prefer Object-Oriented Polymorphism over Functions with
isinstance Checks
- Consider
functools.singledispatch for Functional-Style Programming Instead of Object-Oriented Polymorphism
- Prefer
dataclasses for Defining Lightweight Classes
- Use
@classmethod Polymorphism to Construct Objects Generically
- Initialize Parent Classes with
super
- Consider Composing Functionality with Mix-in Classes
- Prefer Public Attributes over Private Ones
- Prefer
dataclasses for Creating Immutable Objects
- Inherit from
collections.abc Classes for Custom Container Types
Chapter 8: Metaclasses and Attributes
- Use Plain Attributes Instead of Setter and Getter Methods
- Consider
@property Instead of Refactoring Attributes
- Use Descriptors for Reusable
@property Methods
- Use
__getattr__, __getattribute__, and __setattr__ for Lazy Attributes
- Validate Subclasses with
__init_subclass__
- Register Class Existence with
__init_subclass__
- Annotate Class Attributes with
__set_name__
- Consider Class Body Definition Order to Establish Relationships Between Attributes
- Prefer Class Decorators over Metaclasses for Composable Class Extensions
Chapter 9: Concurrency and Parallelism
- Use
subprocess to Manage Child Processes
- Use Threads for Blocking I/O; Avoid for Parallelism
- Use
Lock to Prevent Data Races in Threads
- Use
Queue to Coordinate Work Between Threads
- Know How to Recognize When Concurrency Is Necessary
- Avoid Creating New
Thread Instances for On-demand Fan-out
- Understand How Using
Queue for Concurrency Requires Refactoring
- Consider
ThreadPoolExecutor When Threads Are Necessary for Concurrency
- Achieve Highly Concurrent I/O with Coroutines
- Know How to Port Threaded I/O to
asyncio
- Mix Threads and Coroutines to Ease the Transition to
asyncio
- Maximize Responsiveness of
asyncio Event Loops with async-friendly Worker Threads
- Consider
concurrent.futures for True Parallelism
Chapter 10: Robustness
- Take Advantage of Each Block in
try/ except/ else/ finally
assert Internal Assumptions and raise Missed Expectations
- Consider
contextlib and with Statements for Reusable try/ finally Behavior
- Always Make
try Blocks as Short as Possible
- Beware of Exception Variables Disappearing
- Beware of Catching the
Exception Class
- Understand the Difference Between
Exception and BaseException
- Use
traceback for Enhanced Exception Reporting
- Consider Explicitly Chaining Exceptions to Clarify Tracebacks
- Always Pass Resources into Generators and Have Callers Clean Them Up Outside
- Never Set
__debug__ to False
- Avoid
exec and eval Unless You’re Building a Developer Tool
Chapter 11: Performance
- Profile Before Optimizing
- Optimize Performance-Critical Code Using
timeit Microbenchmarks
- Know When and How to Replace Python with Another Programming Language
- Consider
ctypes to Rapidly Integrate with Native Libraries
- Consider Extension Modules to Maximize Performance and Ergonomics
- Rely on Precompiled Bytecode and File System Caching to Improve Startup Time
- Lazy-Load Modules with Dynamic Imports to Reduce Startup Time
- Consider
memoryview and bytearray for Zero-Copy Interactions with bytes
Chapter 12: Data Structures and Algorithms
- Sort by Complex Criteria Using the
key Parameter
- Know the Difference Between
sort and sorted
- Consider Searching Sorted Sequences with
bisect
- Prefer
deque for Producer-Consumer Queues
- Know How to Use
heapq for Priority Queues
- Use
datetime Instead of time for Local Clocks
- Use
decimal When Precision Is Paramount
- Make
pickle Serialization Maintainable with copyreg
Chapter 13: Testing and Debugging
- Verify Related Behaviors in
TestCase Subclasses
- Prefer Integration Tests over Unit Tests
- Isolate Tests From Each Other with
setUp, tearDown, setUpModule, and tearDownModule
- Use Mocks to Test Code with Complex Dependencies
- Encapsulate Dependencies to Facilitate Mocking and Testing
- Use
assertAlmostEqual to Control Precision in Floating Point Tests
- Consider Interactive Debugging with
pdb
- Use
tracemalloc to Understand Memory Usage and Leaks
Chapter 14: Collaboration
- Know Where to Find Community-Built Modules
- Use Virtual Environments for Isolated and Reproducible Dependencies
- Write Docstrings for Every Function, Class, and Module
- Use Packages to Organize Modules and Provide Stable APIs
- Consider Module-Scoped Code to Configure Deployment Environments
- Define a Root
Exception to Insulate Callers from APIs
- Know How to Break Circular Dependencies
- Consider
warnings to Refactor and Migrate Usage
- Consider Static Analysis via
typing to Obviate Bugs
- 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.
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.
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.
|