O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Effective Python: 90 Specific Ways to Write Better Python, 2nd Edition

Book Description

Updated and Expanded for Python 3

It’s easy to start developing programs with Python, which is why the language is so popular. However, Python’s unique strengths, charms, and expressiveness can be hard to grasp, and there are hidden pitfalls that can easily trip you up.

This second edition of Effective Python will help you master a truly “Pythonic” approach to programming, harnessing Python’s full power to write exceptionally robust and well-performing code. Using the concise, scenario-driven style pioneered in Scott Meyers’ best-selling Effective C++, Brett Slatkin brings together 90 Python best practices, tips, and shortcuts, and explains them with realistic code examples so that you can embrace Python with confidence.

Drawing on years of experience building Python infrastructure at Google, Slatkin uncovers little-known quirks and idioms that powerfully impact code behavior and performance. You’ll understand the best way to accomplish key tasks so you can write code that’s easier to understand, maintain, and improve. In addition to even more advice, this new edition substantially revises all items from the first edition to reflect how best practices have evolved.

Key features include

  • 30 new actionable guidelines for all major areas of Python
  • Detailed explanations and examples of statements, expressions, and built-in types
  • Best practices for writing functions that clarify intention, promote reuse, and avoid bugs
  • Better techniques and idioms for using comprehensions and generator functions
  • Coverage of how to accurately express behaviors with classes and interfaces
  • Guidance on how to avoid pitfalls with metaclasses and dynamic attributes
  • More efficient and clear approaches to concurrency and parallelism
  • Solutions for optimizing and hardening to maximize performance and quality
  • Techniques and built-in modules that aid in debugging and testing
  • Tools and best practices for collaborative development

Effective Python will prepare growing programmers to make a big impact using Python.

Table of Contents

  1. Cover Page
  2. About This eBook
  3. Half Title Page
  4. Title Page
  5. Copyright Page
  6. Dedication Page
  7. Contents at a Glance
  8. Contents
  9. Preface
  10. Acknowledgments
  11. About the Author
  12. 1. Pythonic Thinking
    1. Item 1: Know Which Version of Python You’re Using
    2. Item 2: Follow the PEP 8 Style Guide
    3. Item 3: Know the Differences Between bytes and str
    4. Item 4: Prefer Interpolated F-Strings Over C-style Format Strings and str.format
    5. Item 5: Write Helper Functions Instead of Complex Expressions
    6. Item 6: Prefer Multiple Assignment Unpacking Over Indexing
    7. Item 7: Prefer enumerate Over range
    8. Item 8: Use zip to Process Iterators in Parallel
    9. Item 9: Avoid else Blocks After for and while Loops
    10. Item 10: Prevent Repetition with Assignment Expressions
  13. 2. Lists and Dictionaries
    1. Item 11: Know How to Slice Sequences
    2. Item 12: Avoid Striding and Slicing in a Single Expression
    3. Item 13: Prefer Catch-All Unpacking Over Slicing
    4. Item 14: Sort by Complex Criteria Using the key Parameter
    5. Item 15: Be Cautious When Relying on dict Insertion Ordering
    6. Item 16: Prefer get Over in and KeyError to Handle Missing Dictionary Keys
    7. Item 17: Prefer defaultdict Over setdefault to Handle Missing Items in Internal State
    8. Item 18: Know How to Construct Key-Dependent Default Values with __missing__
  14. 3. Functions
    1. Item 19: Never Unpack More Than Three Variables When Functions Return Multiple Values
    2. Item 20: Prefer Raising Exceptions to Returning None
    3. Item 21: Know How Closures Interact with Variable Scope
    4. Item 22: Reduce Visual Noise with Variable Positional Arguments
    5. Item 23: Provide Optional Behavior with Keyword Arguments
    6. Item 24: Use None and Docstrings to Specify Dynamic Default Arguments
    7. Item 25: Enforce Clarity with Keyword-Only and Positional-Only Arguments
    8. Item 26: Define Function Decorators with functools.wraps
  15. 4. Comprehensions and Generators
    1. Item 27: Use Comprehensions Instead of map and filter
    2. Item 28: Avoid More Than Two Control Subexpressions in Comprehensions
    3. Item 29: Avoid Repeated Work in Comprehensions by Using Assignment Expressions
    4. Item 30: Consider Generators Instead of Returning Lists
    5. Item 31: Be Defensive When Iterating Over Arguments
    6. Item 32: Consider Generator Expressions for Large List Comprehensions
    7. Item 33: Compose Multiple Generators with yield from
    8. Item 34: Avoid Injecting Data into Generators with send
    9. Item 35: Avoid Causing State Transitions in Generators with throw
    10. Item 36: Consider itertools for Working with Iterators and Generators
  16. 5. Classes and Interfaces
    1. Item 37: Compose Classes Instead of Nesting Many Levels of Built-in Types
    2. Item 38: Accept Functions Instead of Classes for Simple Interfaces
    3. Item 39: Use @classmethod Polymorphism to Construct Objects Generically
    4. Item 40: Initialize Parent Classes with super
    5. Item 41: Consider Composing Functionality with Mix-in Classes
    6. Item 42: Prefer Public Attributes Over Private Ones
    7. Item 43: Inherit from collections.abc for Custom Container Types
  17. 6. Metaclasses and Attributes
    1. Item 44: Use Plain Attributes Instead of Setter and Getter Methods
    2. Item 45: Consider @property Instead of Refactoring Attributes
    3. Item 46: Use Descriptors for Reusable @property Methods
    4. Item 47: Use __getattr__, __getattribute__, and __setattr__ for Lazy Attributes
    5. Item 48: Validate Subclasses with __init_subclass__
    6. Item 49: Register Class Existence with __init_subclass__
    7. Item 50: Annotate Class Attributes with __set_name__
    8. Item 51: Prefer Class Decorators Over Metaclasses for Composable Class Extensions
  18. 7. Concurrency and Parallelism
    1. Item 52: Use subprocess to Manage Child Processes
    2. Item 53: Use Threads for Blocking I/O, Avoid for Parallelism
    3. Item 54: Use Lock to Prevent Data Races in Threads
    4. Item 55: Use Queue to Coordinate Work Between Threads
    5. Item 56: Know How to Recognize When Concurrency Is Necessary
    6. Item 57: Avoid Creating New Thread Instances for On-demand Fan-out
    7. Item 58: Understand How Using Queue for Concurrency Requires Refactoring
    8. Item 59: Consider ThreadPoolExecutor When Threads Are Necessary for Concurrency
    9. Item 60: Achieve Highly Concurrent I/O with Coroutines
    10. Item 61: Know How to Port Threaded I/O to asyncio
    11. Item 62: Mix Threads and Coroutines to Ease the Transition to asyncio
    12. Item 63: Avoid Blocking the asyncio Event Loop to Maximize Responsiveness
    13. Item 64: Consider concurrent.futures for True Parallelism
  19. 8. Robustness and Performance
    1. Item 65: Take Advantage of Each Block in try/except/else/finally
    2. Item 66: Consider contextlib and with Statements for Reusable try/finally Behavior
    3. Item 67: Use datetime Instead of time for Local Clocks
    4. Item 68: Make pickle Reliable with copyreg
    5. Item 69: Use decimal When Precision Is Paramount
    6. Item 70: Profile Before Optimizing
    7. Item 71: Prefer deque for Producer–Consumer Queues for Producer–Consumer Queues
    8. Item 72: Consider Searching Sorted Sequences with bisect
    9. Item 73: Know How to Use heapq for Priority Queues
    10. Item 74: Consider memoryview and bytearray for Zero-Copy Interactions with bytes
  20. 9. Testing and Debugging
    1. Item 75: Use repr Strings for Debugging Output
    2. Item 76: Verify Related Behaviors in TestCase Subclasses
    3. Item 77: Isolate Tests from Each Other with setUp, tearDown, setUpModule, and tearDownModule
    4. Item 78: Use Mocks to Test Code with Complex Dependencies
    5. Item 79: Encapsulate Dependencies to Facilitate Mocking and Testing
    6. Item 80: Consider Interactive Debugging with pdb
    7. Item 81: Use tracemalloc to Understand Memory Usage and Leaks
  21. 10. Collaboration
    1. Item 82: Know Where to Find Community-Built Modules
    2. Item 83: Use Virtual Environments for Isolated and Reproducible Dependencies
    3. Item 84: Write Docstrings for Every Function, Class, and Module
    4. Item 85: Use Packages to Organize Modules and Provide Stable APIs
    5. Item 86: Consider Module-Scoped Code to Configure Deployment Environments
    6. Item 87: Define a Root Exception to Insulate Callers from APIs
    7. Item 88: Know How to Break Circular Dependencies
    8. Item 89: Consider warnings to Refactor and Migrate Usage
    9. Item 90: Consider Static Analysis via typing to Obviate Bugs
  22. Index
  23. Code Snippets