You are previewing Fluent Python.
O'Reilly logo
Fluent Python

Book Description

Python’s simplicity lets you become productive quickly, but this often means you aren’t using everything it has to offer. With this hands-on guide, you’ll learn how to write effective, idiomatic Python code by leveraging its best—and possibly most neglected—features. Author Luciano Ramalho takes you through Python’s core language features and libraries, and shows you how to make your code shorter, faster, and more readable at the same time.

Many experienced programmers try to bend Python to fit patterns they learned from other languages, and never discover Python features outside of their experience. With this book, those Python programmers will thoroughly learn how to become proficient in Python 3.

Table of Contents

  1. Dedication
  2. Preface
    1. Who This Book Is For
    2. Who This Book Is Not For
    3. How This Book Is Organized
    4. Hands-On Approach
    5. Hardware Used for Timings
    6. Soapbox: My Personal Perspective
    7. Python Jargon
    8. Python Version Covered
    9. Conventions Used in This Book
    10. Using Code Examples
    11. Safari® Books Online
    12. How to Contact Us
    13. Acknowledgments
  3. I. Prologue
    1. 1. The Python Data Model
      1. A Pythonic Card Deck
      2. How Special Methods Are Used
        1. Emulating Numeric Types
        2. String Representation
        3. Arithmetic Operators
        4. Boolean Value of a Custom Type
      3. Overview of Special Methods
      4. Why len Is Not a Method
      5. Chapter Summary
      6. Further Reading
  4. II. Data Structures
    1. 2. An Array of Sequences
      1. Overview of Built-In Sequences
      2. List Comprehensions and Generator Expressions
        1. List Comprehensions and Readability
        2. Listcomps Versus map and filter
        3. Cartesian Products
        4. Generator Expressions
      3. Tuples Are Not Just Immutable Lists
        1. Tuples as Records
        2. Tuple Unpacking
          1. Using * to grab excess items
        3. Nested Tuple Unpacking
        4. Named Tuples
        5. Tuples as Immutable Lists
      4. Slicing
        1. Why Slices and Range Exclude the Last Item
        2. Slice Objects
        3. Multidimensional Slicing and Ellipsis
        4. Assigning to Slices
      5. Using + and * with Sequences
        1. Building Lists of Lists
      6. Augmented Assignment with Sequences
        1. A += Assignment Puzzler
      7. list.sort and the sorted Built-In Function
      8. Managing Ordered Sequences with bisect
        1. Searching with bisect
        2. Inserting with bisect.insort
      9. When a List Is Not the Answer
        1. Arrays
        2. Memory Views
        3. NumPy and SciPy
        4. Deques and Other Queues
      10. Chapter Summary
      11. Further Reading
    2. 3. Dictionaries and Sets
      1. Generic Mapping Types
      2. dict Comprehensions
      3. Overview of Common Mapping Methods
        1. Handling Missing Keys with setdefault
      4. Mappings with Flexible Key Lookup
        1. defaultdict: Another Take on Missing Keys
        2. The __missing__ Method
      5. Variations of dict
      6. Subclassing UserDict
      7. Immutable Mappings
      8. Set Theory
        1. set Literals
        2. Set Comprehensions
        3. Set Operations
      9. dict and set Under the Hood
        1. A Performance Experiment
        2. Hash Tables in Dictionaries
          1. Hashes and equality
          2. The hash table algorithm
        3. Practical Consequences of How dict Works
          1. Keys must be hashable objects
          2. dicts have significant memory overhead
          3. Key search is very fast
          4. Key ordering depends on insertion order
          5. Adding items to a dict may change the order of existing keys
        4. How Sets Work—Practical Consequences
      10. Chapter Summary
      11. Further Reading
    3. 4. Text versus Bytes
      1. Character Issues
      2. Byte Essentials
        1. Structs and Memory Views
      3. Basic Encoders/Decoders
      4. Understanding Encode/Decode Problems
        1. Coping with UnicodeEncodeError
        2. Coping with UnicodeDecodeError
        3. SyntaxError When Loading Modules with Unexpected Encoding
        4. How to Discover the Encoding of a Byte Sequence
        5. BOM: A Useful Gremlin
      5. Handling Text Files
        1. Encoding Defaults: A Madhouse
      6. Normalizing Unicode for Saner Comparisons
        1. Case Folding
        2. Utility Functions for Normalized Text Matching
        3. Extreme “Normalization”: Taking Out Diacritics
      7. Sorting Unicode Text
        1. Sorting with the Unicode Collation Algorithm
      8. The Unicode Database
      9. Dual-Mode str and bytes APIs
        1. str Versus bytes in Regular Expressions
        2. str Versus bytes on os Functions
      10. Chapter Summary
      11. Further Reading
  5. III. Functions as Objects
    1. 5. First-Class Functions
      1. Treating a Function Like an Object
      2. Higher-Order Functions
        1. Modern Replacements for map, filter, and reduce
      3. Anonymous Functions
      4. The Seven Flavors of Callable Objects
      5. User-Defined Callable Types
      6. Function Introspection
      7. From Positional to Keyword-Only Parameters
      8. Retrieving Information About Parameters
      9. Function Annotations
      10. Packages for Functional Programming
        1. The operator Module
        2. Freezing Arguments with functools.partial
      11. Chapter Summary
      12. Further Reading
    2. 6. Design Patterns with First-Class Functions
      1. Case Study: Refactoring Strategy
        1. Classic Strategy
        2. Function-Oriented Strategy
        3. Choosing the Best Strategy: Simple Approach
        4. Finding Strategies in a Module
      2. Command
      3. Chapter Summary
      4. Further Reading
    3. 7. Function Decorators and Closures
      1. Decorators 101
      2. When Python Executes Decorators
      3. Decorator-Enhanced Strategy Pattern
      4. Variable Scope Rules
      5. Closures
      6. The nonlocal Declaration
      7. Implementing a Simple Decorator
        1. How It Works
      8. Decorators in the Standard Library
        1. Memoization with functools.lru_cache
        2. Generic Functions with Single Dispatch
      9. Stacked Decorators
      10. Parameterized Decorators
        1. A Parameterized Registration Decorator
        2. The Parameterized Clock Decorator
      11. Chapter Summary
      12. Further Reading
  6. IV. Object-Oriented Idioms
    1. 8. Object References, Mutability, and Recycling
      1. Variables Are Not Boxes
      2. Identity, Equality, and Aliases
        1. Choosing Between == and is
        2. The Relative Immutability of Tuples
      3. Copies Are Shallow by Default
        1. Deep and Shallow Copies of Arbitrary Objects
      4. Function Parameters as References
        1. Mutable Types as Parameter Defaults: Bad Idea
        2. Defensive Programming with Mutable Parameters
      5. del and Garbage Collection
      6. Weak References
        1. The WeakValueDictionary Skit
        2. Limitations of Weak References
      7. Tricks Python Plays with Immutables
      8. Chapter Summary
      9. Further Reading
    2. 9. A Pythonic Object
      1. Object Representations
      2. Vector Class Redux
      3. An Alternative Constructor
      4. classmethod Versus staticmethod
      5. Formatted Displays
      6. A Hashable Vector2d
      7. Private and “Protected” Attributes in Python
      8. Saving Space with the __slots__ Class Attribute
        1. The Problems with __slots__
      9. Overriding Class Attributes
      10. Chapter Summary
      11. Further Reading
    3. 10. Sequence Hacking, Hashing, and Slicing
      1. Vector: A User-Defined Sequence Type
      2. Vector Take #1: Vector2d Compatible
      3. Protocols and Duck Typing
      4. Vector Take #2: A Sliceable Sequence
        1. How Slicing Works
        2. A Slice-Aware __getitem__
      5. Vector Take #3: Dynamic Attribute Access
      6. Vector Take #4: Hashing and a Faster ==
      7. Vector Take #5: Formatting
      8. Chapter Summary
      9. Further Reading
    4. 11. Interfaces: From Protocols to ABCs
      1. Interfaces and Protocols in Python Culture
      2. Python Digs Sequences
      3. Monkey-Patching to Implement a Protocol at Runtime
      4. Alex Martelli’s Waterfowl
      5. Subclassing an ABC
      6. ABCs in the Standard Library
        1. ABCs in collections.abc
        2. The Numbers Tower of ABCs
      7. Defining and Using an ABC
        1. ABC Syntax Details
        2. Subclassing the Tombola ABC
        3. A Virtual Subclass of Tombola
      8. How the Tombola Subclasses Were Tested
      9. Usage of register in Practice
      10. Geese Can Behave as Ducks
      11. Chapter Summary
      12. Further Reading
    5. 12. Inheritance: For Good or For Worse
      1. Subclassing Built-In Types Is Tricky
      2. Multiple Inheritance and Method Resolution Order
      3. Multiple Inheritance in the Real World
      4. Coping with Multiple Inheritance
        1. 1. Distinguish Interface Inheritance from Implementation Inheritance
        2. 2. Make Interfaces Explicit with ABCs
        3. 3. Use Mixins for Code Reuse
        4. 4. Make Mixins Explicit by Naming
        5. 5. An ABC May Also Be a Mixin; The Reverse Is Not True
        6. 6. Don’t Subclass from More Than One Concrete Class
        7. 7. Provide Aggregate Classes to Users
        8. 8. “Favor Object Composition Over Class Inheritance.”
        9. Tkinter: The Good, the Bad, and the Ugly
      5. A Modern Example: Mixins in Django Generic Views
      6. Chapter Summary
      7. Further Reading
    6. 13. Operator Overloading: Doing It Right
      1. Operator Overloading 101
      2. Unary Operators
      3. Overloading + for Vector Addition
      4. Overloading * for Scalar Multiplication
      5. Rich Comparison Operators
      6. Augmented Assignment Operators
      7. Chapter Summary
      8. Further Reading
  7. V. Control Flow
    1. 14. Iterables, Iterators, and Generators
      1. Sentence Take #1: A Sequence of Words
        1. Why Sequences Are Iterable: The iter Function
      2. Iterables Versus Iterators
      3. Sentence Take #2: A Classic Interior
        1. Making Sentence an Iterator: Bad Idea
      4. Sentence Take #3: A Generator Function
        1. How a Generator Function Works
      5. Sentence Take #4: A Lazy Implementation
      6. Sentence Take #5: A Generator Expression
      7. Generator Expressions: When to Use Them
      8. Another Example: Arithmetic Progression Generator
        1. Arithmetic Progression with itertools
      9. Generator Functions in the Standard Library
      10. New Syntax in Python 3.3: yield from
      11. Iterable Reducing Functions
      12. A Closer Look at the iter Function
      13. Case Study: Generators in a Database Conversion Utility
      14. Generators as Coroutines
      15. Chapter Summary
      16. Further Reading
    2. 15. Context Managers and else Blocks
      1. Do This, Then That: else Blocks Beyond if
      2. Context Managers and with Blocks
      3. The contextlib Utilities
      4. Using @contextmanager
      5. Chapter Summary
      6. Further Reading
    3. 16. Coroutines
      1. How Coroutines Evolved from Generators
      2. Basic Behavior of a Generator Used as a Coroutine
      3. Example: Coroutine to Compute a Running Average
      4. Decorators for Coroutine Priming
      5. Coroutine Termination and Exception Handling
      6. Returning a Value from a Coroutine
      7. Using yield from
      8. The Meaning of yield from
      9. Use Case: Coroutines for Discrete Event Simulation
        1. About Discrete Event Simulations
        2. The Taxi Fleet Simulation
      10. Chapter Summary
      11. Further Reading
    4. 17. Concurrency with Futures
      1. Example: Web Downloads in Three Styles
        1. A Sequential Download Script
        2. Downloading with concurrent.futures
        3. Where Are the Futures?
      2. Blocking I/O and the GIL
      3. Launching Processes with concurrent.futures
      4. Experimenting with Executor.map
      5. Downloads with Progress Display and Error Handling
        1. Error Handling in the flags2 Examples
        2. Using futures.as_completed
        3. Threading and Multiprocessing Alternatives
      6. Chapter Summary
      7. Further Reading
    5. 18. Concurrency with asyncio
      1. Thread Versus Coroutine: A Comparison
        1. asyncio.Future: Nonblocking by Design
        2. Yielding from Futures, Tasks, and Coroutines
      2. Downloading with asyncio and aiohttp
      3. Running Circling Around Blocking Calls
      4. Enhancing the asyncio downloader Script
        1. Using asyncio.as_completed
        2. Using an Executor to Avoid Blocking the Event Loop
      5. From Callbacks to Futures and Coroutines
        1. Doing Multiple Requests for Each Download
      6. Writing asyncio Servers
        1. An asyncio TCP Server
        2. An aiohttp Web Server
        3. Smarter Clients for Better Concurrency
      7. Chapter Summary
      8. Further Reading
  8. VI. Metaprogramming
    1. 19. Dynamic Attributes and Properties
      1. Data Wrangling with Dynamic Attributes
        1. Exploring JSON-Like Data with Dynamic Attributes
        2. The Invalid Attribute Name Problem
        3. Flexible Object Creation with __new__
        4. Restructuring the OSCON Feed with shelve
        5. Linked Record Retrieval with Properties
      2. Using a Property for Attribute Validation
        1. LineItem Take #1: Class for an Item in an Order
        2. LineItem Take #2: A Validating Property
      3. A Proper Look at Properties
        1. Properties Override Instance Attributes
        2. Property Documentation
      4. Coding a Property Factory
      5. Handling Attribute Deletion
      6. Essential Attributes and Functions for Attribute Handling
        1. Special Attributes that Affect Attribute Handling
        2. Built-In Functions for Attribute Handling
        3. Special Methods for Attribute Handling
      7. Chapter Summary
      8. Further Reading
    2. 20. Attribute Descriptors
      1. Descriptor Example: Attribute Validation
        1. LineItem Take #3: A Simple Descriptor
        2. LineItem Take #4: Automatic Storage Attribute Names
        3. LineItem Take #5: A New Descriptor Type
      2. Overriding Versus Nonoverriding Descriptors
        1. Overriding Descriptor
        2. Overriding Descriptor Without __get__
        3. Nonoverriding Descriptor
        4. Overwriting a Descriptor in the Class
      3. Methods Are Descriptors
      4. Descriptor Usage Tips
      5. Descriptor docstring and Overriding Deletion
      6. Chapter Summary
      7. Further Reading
    3. 21. Class Metaprogramming
      1. A Class Factory
      2. A Class Decorator for Customizing Descriptors
      3. What Happens When: Import Time Versus Runtime
        1. The Evaluation Time Exercises
          1. Solution for scenario #1
          2. Solution for scenario #2
      4. Metaclasses 101
        1. The Metaclass Evaluation Time Exercise
          1. Solution for scenario #3
          2. Solution for scenario #4
      5. A Metaclass for Customizing Descriptors
      6. The Metaclass __prepare__ Special Method
      7. Classes as Objects
      8. Chapter Summary
      9. Further Reading
    4. Afterword
      1. Further Reading
    5. A. Support Scripts
      1. Chapter 3: in Operator Performance Test
      2. Chapter 3: Compare the Bit Patterns of Hashes
      3. Chapter 9: RAM Usage With and Without __slots__
      4. Chapter 14: isis2json.py Database Conversion Script
      5. Chapter 16: Taxi Fleet Discrete Event Simulation
      6. Chapter 17: Cryptographic Examples
      7. Chapter 17: flags2 HTTP Client Examples
      8. Chapter 19: OSCON Schedule Scripts and Tests
    6. Python Jargon
  9. Index
  10. Colophon
  11. Copyright