0%

Book Description

Python’s simplicity lets you become productive quickly, but often this means you aren’t using everything it has to offer. With the updated edition of this hands-on guide, you’ll learn how to write effective, modern Python 3 code by leveraging its best ideas.

Don’t waste time bending Python to fit patterns you learned in other languages. Discover and apply idiomatic Python 3 features beyond your past experience. Author Luciano Ramalho guides you through Python’s core language features and libraries and teaches you how to make your code shorter, faster, and more readable.

Featuring major updates throughout the book, Fluent Python, second edition, covers:

  • Special methods: The key to the consistent behavior of Python objects
  • Data structures: Sequences, dicts, sets, Unicode, and data classes
  • Functions as objects: First-class functions, related design patterns, and type hints in function declarations
  • Object-oriented idioms: Composition, inheritance, mixins, interfaces, operator overloading, static typing and protocols
  • Control flow: Context managers, generators, coroutines, async/await, and thread/process pools
  • Metaprogramming: Properties, attribute descriptors, class decorators, and new class metaprogramming hooks that are simpler than metaclasses

Table of Contents

  1. 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. How to Contact Us
    12. Acknowledgments
  2. I. Prologue
  3. 1. The Python Data Model
    1. What’s new in this chapter
    2. A Pythonic Card Deck
    3. How Special Methods Are Used
      1. Emulating Numeric Types
      2. String Representation
      3. Arithmetic Operators
      4. Boolean Value of a Custom Type
      5. Collection API
    4. Overview of Special Methods
    5. Why len Is Not a Method
    6. Chapter Summary
    7. Further Reading
  4. II. Data Structures
  5. 2. An Array of Sequences
    1. What’s new in this chapter
    2. Overview of Built-In Sequences
    3. List Comprehensions and Generator Expressions
      1. List Comprehensions and Readability
      2. Listcomps Versus map and filter
      3. Cartesian Products
      4. Generator Expressions
    4. Tuples Are Not Just Immutable Lists
      1. Tuples as Records
      2. Unpacking
      3. Nested Tuple Unpacking
      4. Tuples as Immutable Lists
      5. Tuple versus list methods
    5. Slicing
      1. Why Slices and Range Exclude the Last Item
      2. Slice Objects
      3. Multidimensional Slicing and Ellipsis
      4. Assigning to Slices
    6. Using + and * with Sequences
      1. Building Lists of Lists
    7. Augmented Assignment with Sequences
      1. A += Assignment Puzzler
    8. list.sort and the sorted Built-In Function
    9. Managing Ordered Sequences with bisect
      1. Searching with bisect
      2. Inserting with bisect.insort
    10. When a List Is Not the Answer
      1. Arrays
      2. Memory Views
      3. NumPy
      4. Deques and Other Queues
    11. Chapter Summary
    12. Further Reading
  6. 3. Dictionaries and Sets
    1. What’s new in this chapter
    2. Standard API of Mapping Types
    3. dict Comprehensions
    4. Overview of Common Mapping Methods
      1. Handling Missing Keys with setdefault
    5. Mappings with Flexible Key Lookup
      1. defaultdict: Another Take on Missing Keys
      2. The __missing__ Method
    6. Variations of dict
    7. Building custom mappings
      1. Subclassing UserDict
    8. Immutable Mappings
    9. Dictionary views
    10. Set Theory
      1. Set Literals
      2. Set Comprehensions
      3. Set Operations
      4. Set operations on dict views
    11. Internals of sets and dicts
      1. A Performance Experiment
      2. Set hash tables under the hood
      3. The hash table algorithm
      4. Hash table usage in dict
      5. Key-sharing dictionary
      6. Practical Consequences of How dict Works
    12. Chapter Summary
    13. Further Reading
  7. 4. Text versus Bytes
    1. What’s new in this chapter
    2. Character Issues
    3. Byte Essentials
    4. Basic Encoders/Decoders
    5. 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
    6. Handling Text Files
      1. Beware of Encoding Defaults
    7. Normalizing Unicode for Reliable Comparisons
      1. Case Folding
      2. Utility Functions for Normalized Text Matching
      3. Extreme “Normalization”: Taking Out Diacritics
    8. Sorting Unicode Text
      1. Sorting with the Unicode Collation Algorithm
    9. The Unicode Database
      1. Finding characters by name
      2. Numeric meaning of characters
    10. Dual-Mode str and bytes APIs
      1. str Versus bytes in Regular Expressions
      2. str Versus bytes in os Functions
    11. Multi-character emojis
      1. Country flags
      2. Skin tones
      3. Rainbow flag and other ZWJ sequences
    12. Chapter Summary
    13. Further Reading
  8. 5. Record-like data structures
    1. What’s new in this chapter
    2. Overview of data class builders
      1. Main features
    3. Classic Named Tuples
    4. Typed Named Tuples
    5. Type hints 101
      1. No runtime effect
      2. Variable annotation Syntax
      3. The meaning of variable annotations
    6. More about @dataclass
      1. Field options
      2. Post-init processing
      3. Typed class attributes
      4. Initialization variables that are not fields
      5. @dataclass Example: Dublin Core Resource Record
    7. Data class as a code smell
      1. Data class as scaffolding
      2. Data class as intermediate representation
    8. Parsing binary records with struct
      1. Structs and Memory Views
      2. Should we use struct?
    9. Chapter Summary
    10. Further Reading
  9. 6. Object References, Mutability, and Recycling
    1. What’s new in this chapter
    2. Variables Are Not Boxes
    3. Identity, Equality, and Aliases
      1. Choosing Between == and is
      2. The Relative Immutability of Tuples
    4. Copies Are Shallow by Default
      1. Deep and Shallow Copies of Arbitrary Objects
    5. Function Parameters as References
      1. Mutable Types as Parameter Defaults: Bad Idea
      2. Defensive Programming with Mutable Parameters
    6. del and Garbage Collection
    7. Weak References
      1. The WeakValueDictionary Skit
      2. Limitations of Weak References
    8. Tricks Python Plays with Immutables
    9. Chapter Summary
    10. Further Reading
  10. III. Functions as Objects
  11. 7. First-Class Functions
    1. What’s new in this chapter
    2. Treating a Function Like an Object
    3. Higher-Order Functions
      1. Modern Replacements for map, filter, and reduce
    4. Anonymous Functions
    5. The Nine Flavors of Callable Objects
    6. User-Defined Callable Types
    7. Function Introspection
    8. From Positional to Keyword-Only Parameters
      1. Positional-only parameters
    9. Retrieving Information About Parameters
    10. Packages for Functional Programming
      1. The operator Module
      2. Freezing Arguments with functools.partial
    11. Chapter Summary
    12. Further Reading
  12. 8. Type Hints in Functions
    1. What’s new in this chapter
    2. About gradual typing
    3. Gradual typing in practice
      1. Starting with Mypy
      2. Making Mypy More Strict
      3. A Default Parameter Value
      4. Using None as a default
      5. Type hints for Python 2.7 and 3.x
    4. Types are defined by supported operations
    5. Types usable in annotations
      1. The Any type
      2. Simple types and classes
      3. Optional and Union types
      4. Generic collections
      5. Tuple
      6. Generic mappings
      7. TypedDict
      8. Abstract Base Classes
      9. Iterable
      10. Parameterized generics and TypeVar
      11. Protocols
      12. Callable
      13. NoReturn
    6. Overloaded signatures
    7. Annotating positional-only and variadic parameters
    8. Reading annotations at runtime
    9. Chapter summary
    10. Further Reading
  13. 9. Decorators and Closures
    1. What’s new in this chapter
    2. Decorators 101
    3. When Python Executes Decorators
    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. Single Dispatch Generic Functions
    9. Parameterized Decorators
      1. A Parameterized Registration Decorator
      2. The Parameterized Clock Decorator
    10. Chapter Summary
    11. Further Reading
  14. 10. Design Patterns with First-Class Functions
    1. What’s new in this chapter
    2. Case Study: Refactoring Strategy
      1. Classic Strategy
      2. Function-Oriented Strategy
      3. Choosing the Best Strategy: Simple Approach
      4. Finding Strategies in a Module
    3. Decorator-Enhanced Strategy Pattern
    4. Command
    5. Chapter Summary
    6. Further Reading
  15. IV. Classes and Protocols
  16. 11. A Pythonic Object
    1. What’s new in this chapter
    2. Object Representations
    3. Vector Class Redux
    4. An Alternative Constructor
    5. classmethod Versus staticmethod
    6. Formatted Displays
    7. A Hashable Vector2d
    8. Private and “Protected” Attributes in Python
    9. Saving Memory with __slots__
      1. The Problems with __slots__
    10. Overriding Class Attributes
    11. Implementing a typing protocol
    12. Chapter Summary
    13. Further Reading
  17. 12. Sequence Hacking, Hashing, and Slicing
    1. What’s new in this chapter
    2. Vector: A User-Defined Sequence Type
    3. Vector Take #1: Vector2d Compatible
    4. Protocols and Duck Typing
    5. Vector Take #2: A Sliceable Sequence
      1. How Slicing Works
      2. A Slice-Aware __getitem__
    6. Vector Take #3: Dynamic Attribute Access
    7. Vector Take #4: Hashing and a Faster ==
    8. Vector Take #5: Formatting
    9. Chapter Summary
    10. Further Reading
18.206.76.45