| CARVIEW |
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
matchfor Destructuring in Flow Control; Avoid WhenifStatements Are Sufficient
Chapter 2: Strings and Slicing
- Know the Differences Between
bytesandstr - Prefer Interpolated F-Strings over C-Style Format Strings and
str.format - Understand the Difference Between
reprandstrwhen 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
enumerateoverrange - Use
zipto Process Iterators in Parallel - Avoid
elseBlocks AfterforandwhileLoops - Never Use
forLoop 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
anyandallfor Efficient Short-Circuiting Logic - Consider
itertoolsfor Working with Iterators and Generators
Chapter 4: Dictionaries
- Be Cautious when Relying on Dictionary Insertion Ordering
- Prefer
getoverinandKeyErrorto Handle Missing Dictionary Keys - Prefer
defaultdictoversetdefaultto 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
Noneand Docstrings to Specify Dynamic Default Arguments - Enforce Clarity with Keyword-Only and Positional-Only Arguments
- Define Function Decorators with
functools.wraps - Prefer
functools.partialoverlambdaExpressions for Glue Functions
Chapter 6: Comprehensions and Generators
- Use Comprehensions Instead of
mapandfilter - 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
sendMethod - Manage Iterative State Transitions with a Class Instead of the Generator
throwMethod
Chapter 7: Classes and Interfaces
- Accept Functions Instead of Classes for Simple Interfaces
- Prefer Object-Oriented Polymorphism over Functions with
isinstanceChecks - Consider
functools.singledispatchfor Functional-Style Programming Instead of Object-Oriented Polymorphism - Prefer
dataclassesfor Defining Lightweight Classes - Use
@classmethodPolymorphism to Construct Objects Generically - Initialize Parent Classes with
super - Consider Composing Functionality with Mix-in Classes
- Prefer Public Attributes over Private Ones
- Prefer
dataclassesfor Creating Immutable Objects - Inherit from
collections.abcClasses for Custom Container Types
Chapter 8: Metaclasses and Attributes
- Use Plain Attributes Instead of Setter and Getter Methods
- Consider
@propertyInstead of Refactoring Attributes - Use Descriptors for Reusable
@propertyMethods - 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
subprocessto Manage Child Processes - Use Threads for Blocking I/O; Avoid for Parallelism
- Use
Lockto Prevent Data Races in Threads - Use
Queueto Coordinate Work Between Threads - Know How to Recognize When Concurrency Is Necessary
- Avoid Creating New
ThreadInstances for On-demand Fan-out - Understand How Using
Queuefor Concurrency Requires Refactoring - Consider
ThreadPoolExecutorWhen 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
asyncioEvent Loops withasync-friendly Worker Threads - Consider
concurrent.futuresfor True Parallelism
Chapter 10: Robustness
- Take Advantage of Each Block in
try/except/else/finally assertInternal Assumptions andraiseMissed Expectations- Consider
contextlibandwithStatements for Reusabletry/finallyBehavior - Always Make
tryBlocks as Short as Possible - Beware of Exception Variables Disappearing
- Beware of Catching the
ExceptionClass - Understand the Difference Between
ExceptionandBaseException - Use
tracebackfor 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__toFalse - Avoid
execandevalUnless You’re Building a Developer Tool
Chapter 11: Performance
- Profile Before Optimizing
- Optimize Performance-Critical Code Using
timeitMicrobenchmarks - Know When and How to Replace Python with Another Programming Language
- Consider
ctypesto 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
memoryviewandbytearrayfor Zero-Copy Interactions withbytes
Chapter 12: Data Structures and Algorithms
- Sort by Complex Criteria Using the
keyParameter - Know the Difference Between
sortandsorted - Consider Searching Sorted Sequences with
bisect - Prefer
dequefor Producer-Consumer Queues - Know How to Use
heapqfor Priority Queues - Use
datetimeInstead oftimefor Local Clocks - Use
decimalWhen Precision Is Paramount - Make
pickleSerialization Maintainable withcopyreg
Chapter 13: Testing and Debugging
- Verify Related Behaviors in
TestCaseSubclasses - Prefer Integration Tests over Unit Tests
- Isolate Tests From Each Other with
setUp,tearDown,setUpModule, andtearDownModule - Use Mocks to Test Code with Complex Dependencies
- Encapsulate Dependencies to Facilitate Mocking and Testing
- Use
assertAlmostEqualto Control Precision in Floating Point Tests - Consider Interactive Debugging with
pdb - Use
tracemallocto 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
Exceptionto Insulate Callers from APIs - Know How to Break Circular Dependencies
- Consider
warningsto Refactor and Migrate Usage - Consider Static Analysis via
typingto Obviate Bugs - Prefer Open Source Projects for Bundling Python Programs over
zipimportandzipapp
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.
Preorder the Third Edition
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.
