Book description
Don't waste time bending Python to fit patterns you've learned in other languages. Python's simplicity lets you become productive quickly, but often this means you aren't using everything the language 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.
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.
Complete with major updates throughout, this new edition features five parts that work as five short books within the book:
- 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, protocols, and more static types
- Control flow: Context managers, generators, coroutines, async/await, and thread/process pools
- Metaprogramming: Properties, attribute descriptors, class decorators, and new class metaprogramming hooks that replace or simplify metaclasses
Publisher resources
Table of contents
- Preface
- I. Data Structures
- 1. The Python Data Model
-
2. An Array of Sequences
- What’s New in This Chapter
- Overview of Built-In Sequences
- List Comprehensions and Generator Expressions
- Tuples Are Not Just Immutable Lists
- Unpacking Sequences and Iterables
- Pattern Matching with Sequences
- Slicing
- Using + and * with Sequences
- list.sort Versus the sorted Built-In
- When a List Is Not the Answer
- Chapter Summary
- Further Reading
-
3. Dictionaries and Sets
- What’s New in This Chapter
- Modern dict Syntax
- Pattern Matching with Mappings
- Standard API of Mapping Types
- Automatic Handling of Missing Keys
- Variations of dict
- Immutable Mappings
- Dictionary Views
- Practical Consequences of How dict Works
- Set Theory
- Practical Consequences of How Sets Work
- Set Operations on dict Views
- Chapter Summary
- Further Reading
- 4. Unicode Text Versus Bytes
- 5. Data Class Builders
- 6. Object References, Mutability, and Recycling
- II. Functions as Objects
- 7. Functions as First-Class Objects
- 8. Type Hints in Functions
- 9. Decorators and Closures
- 10. Design Patterns with First-Class Functions
- III. Classes and Protocols
-
11. A Pythonic Object
- What’s New in This Chapter
- Object Representations
- Vector Class Redux
- An Alternative Constructor
- classmethod Versus staticmethod
- Formatted Displays
- A Hashable Vector2d
- Supporting Positional Pattern Matching
- Complete Listing of Vector2d, Version 3
- Private and “Protected” Attributes in Python
- Saving Memory with __slots__
- Overriding Class Attributes
- Chapter Summary
- Further Reading
-
12. Special Methods for Sequences
- What’s New in This Chapter
- Vector: A User-Defined Sequence Type
- Vector Take #1: Vector2d Compatible
- Protocols and Duck Typing
- Vector Take #2: A Sliceable Sequence
- Vector Take #3: Dynamic Attribute Access
- Vector Take #4: Hashing and a Faster ==
- Vector Take #5: Formatting
- Chapter Summary
- Further Reading
- 13. Interfaces, Protocols, and ABCs
-
14. Inheritance: For Better or for Worse
- What’s New in This Chapter
- The super() Function
- Subclassing Built-In Types Is Tricky
- Multiple Inheritance and Method Resolution Order
- Mixin Classes
- Multiple Inheritance in the Real World
-
Coping with Inheritance
- Favor Object Composition over Class Inheritance
- Understand Why Inheritance Is Used in Each Case
- Make Interfaces Explicit with ABCs
- Use Explicit Mixins for Code Reuse
- Provide Aggregate Classes to Users
- Subclass Only Classes Designed for Subclassing
- Avoid Subclassing from Concrete Classes
- Tkinter: The Good, the Bad, and the Ugly
- Chapter Summary
- Further Reading
- 15. More About Type Hints
- 16. Operator Overloading
- IV. Control Flow
-
17. Iterators, Generators,
and Classic Coroutines
- What’s New in This Chapter
- A Sequence of Words
- Why Sequences Are Iterable: The iter Function
- Iterables Versus Iterators
- Sentence Classes with __iter__
- Lazy Sentences
- When to Use Generator Expressions
- An Arithmetic Progression Generator
- Generator Functions in the Standard Library
- Iterable Reducing Functions
- Subgenerators with yield from
- Generic Iterable Types
- Classic Coroutines
- Chapter Summary
- Further Reading
- 18. with, match, and else Blocks
- 19. Concurrency Models in Python
- 20. Concurrent Executors
-
21. Asynchronous Programming
- What’s New in This Chapter
- A Few Definitions
- An asyncio Example: Probing Domains
- New Concept: Awaitable
- Downloading with asyncio and HTTPX
- Asynchronous Context Managers
- Enhancing the asyncio Downloader
- Delegating Tasks to Executors
- Writing asyncio Servers
- Asynchronous Iteration and Asynchronous Iterables
- async Beyond asyncio: Curio
- Type Hinting Asynchronous Objects
- How Async Works and How It Doesn’t
- Chapter Summary
- Further Reading
- V. Metaprogramming
- 22. Dynamic Attributes and Properties
- 23. Attribute Descriptors
-
24. Class Metaprogramming
- What’s New in This Chapter
- Classes as Objects
- type: The Built-In Class Factory
- A Class Factory Function
- Introducing __init_subclass__
- Enhancing Classes with a Class Decorator
- What Happens When: Import Time Versus Runtime
- Metaclasses 101
- A Metaclass Solution for Checked
- Metaclasses in the Real World
- A Metaclass Hack with __prepare__
- Wrapping Up
- Chapter Summary
- Further Reading
- Afterword
- Index
- About the Author
Product information
- Title: Fluent Python, 2nd Edition
- Author(s):
- Release date: April 2022
- Publisher(s): O'Reilly Media, Inc.
- ISBN: 9781492056355
You might also like
book
Head First Python, 3rd Edition
What will you learn from this book? Want to learn the Python language without slogging your …
book
Head First Python, 2nd Edition
Want to learn the Python language without slogging your way through how-to manuals? With Head First …
book
Introducing Python, 2nd Edition
Easy to understand and fun to read, this updated edition of Introducing Python is ideal for …
book
Python Crash Course, 3rd Edition
Python Crash Course is the world's best-selling guide to the Python guide programming language, with over …