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.
This book covers:
- Python data model: understand how special methods are the key to the consistent behavior of objects
- Data structures: take full advantage of built-in types, and understand the text vs bytes duality in the Unicode age
- Functions as objects: view Python functions as first-class objects, and understand how this affects popular design patterns
- Object-oriented idioms: build classes by learning about references, mutability, interfaces, operator overloading, and multiple inheritance
- Control flow: leverage context managers, generators, coroutines, and concurrency with the concurrent.futures and asyncio packages
- Metaprogramming: understand how properties, attribute descriptors, class decorators, and metaclasses work
Publisher resources
Table of contents
- Preface
- I. Prologue
- 1. The Python Data Model
- II. Data Structures
-
2. An Array of Sequences
- Overview of Built-In Sequences
- List Comprehensions and Generator Expressions
- Tuples Are Not Just Immutable Lists
- Slicing
- Using + and * with Sequences
- Augmented Assignment with Sequences
- list.sort and the sorted Built-In Function
- Managing Ordered Sequences with bisect
- When a List Is Not the Answer
- Chapter Summary
- Further Reading
- 3. Dictionaries and Sets
- 4. Text versus Bytes
- III. Functions as Objects
-
5. First-Class Functions
- Treating a Function Like an Object
- Higher-Order Functions
- Anonymous Functions
- The Seven Flavors of Callable Objects
- User-Defined Callable Types
- Function Introspection
- From Positional to Keyword-Only Parameters
- Retrieving Information About Parameters
- Function Annotations
- Packages for Functional Programming
- Chapter Summary
- Further Reading
- 6. Design Patterns with First-Class Functions
- 7. Function Decorators and Closures
- IV. Object-Oriented Idioms
- 8. Object References, Mutability, and Recycling
- 9. A Pythonic Object
- 10. Sequence Hacking, Hashing, and Slicing
-
11. Interfaces: From Protocols to ABCs
- Interfaces and Protocols in Python Culture
- Python Digs Sequences
- Monkey-Patching to Implement a Protocol at Runtime
- Alex Martelli’s Waterfowl
- Subclassing an ABC
- ABCs in the Standard Library
- Defining and Using an ABC
- How the Tombola Subclasses Were Tested
- Usage of register in Practice
- Geese Can Behave as Ducks
- Chapter Summary
- Further Reading
-
12. Inheritance: For Good or For Worse
- Subclassing Built-In Types Is Tricky
- Multiple Inheritance and Method Resolution Order
- Multiple Inheritance in the Real World
-
Coping with Multiple Inheritance
- 1. Distinguish Interface Inheritance from Implementation Inheritance
- 2. Make Interfaces Explicit with ABCs
- 3. Use Mixins for Code Reuse
- 4. Make Mixins Explicit by Naming
- 5. An ABC May Also Be a Mixin; The Reverse Is Not True
- 6. Don’t Subclass from More Than One Concrete Class
- 7. Provide Aggregate Classes to Users
- 8. “Favor Object Composition Over Class Inheritance.”
- Tkinter: The Good, the Bad, and the Ugly
- A Modern Example: Mixins in Django Generic Views
- Chapter Summary
- Further Reading
- 13. Operator Overloading: Doing It Right
- V. Control Flow
-
14. Iterables, Iterators, and Generators
- Sentence Take #1: A Sequence of Words
- Iterables Versus Iterators
- Sentence Take #2: A Classic Iterator
- Sentence Take #3: A Generator Function
- Sentence Take #4: A Lazy Implementation
- Sentence Take #5: A Generator Expression
- Generator Expressions: When to Use Them
- Another Example: Arithmetic Progression Generator
- Generator Functions in the Standard Library
- New Syntax in Python 3.3: yield from
- Iterable Reducing Functions
- A Closer Look at the iter Function
- Case Study: Generators in a Database Conversion Utility
- Generators as Coroutines
- Chapter Summary
- Further Reading
- 15. Context Managers and else Blocks
-
16. Coroutines
- How Coroutines Evolved from Generators
- Basic Behavior of a Generator Used as a Coroutine
- Example: Coroutine to Compute a Running Average
- Decorators for Coroutine Priming
- Coroutine Termination and Exception Handling
- Returning a Value from a Coroutine
- Using yield from
- The Meaning of yield from
- Use Case: Coroutines for Discrete Event Simulation
- Chapter Summary
- Further Reading
- 17. Concurrency with Futures
- 18. Concurrency with asyncio
- VI. Metaprogramming
- 19. Dynamic Attributes and Properties
- 20. Attribute Descriptors
- 21. Class Metaprogramming
- Afterword
-
A. Support Scripts
- Chapter 3: in Operator Performance Test
- Chapter 3: Compare the Bit Patterns of Hashes
- Chapter 9: RAM Usage With and Without __slots__
- Chapter 14: isis2json.py Database Conversion Script
- Chapter 16: Taxi Fleet Discrete Event Simulation
- Chapter 17: Cryptographic Examples
- Chapter 17: flags2 HTTP Client Examples
- Chapter 19: OSCON Schedule Scripts and Tests
- Python Jargon
- Index
Product information
- Title: Fluent Python
- Author(s):
- Release date: August 2015
- Publisher(s): O'Reilly Media, Inc.
- ISBN: 9781491946008
You might also like
book
Fluent Python, 2nd Edition
Don't waste time bending Python to fit patterns you've learned in other languages. Python's simplicity lets …
book
Test-Driven Development with Python, 2nd Edition
By taking you through the development of a real web application from beginning to end, the …
book
Using Asyncio in Python
If you’re among the Python developers put off by asyncio’s complexity, it’s time to take another …
book
Robust Python
Does it seem like your Python projects are getting bigger and bigger? Are you feeling the …