Table of Contents
Effective Python: 90 Specific Ways to Write Better Python (Effective Software Development Series) 2nd Edition
Return to Python Bibliography, IT Bibliography, Python
by Slatkin Brett
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.
About the Author Brett Slatkin is a principal software engineer at Google. He is the technical co-founder of Google Surveys, the co-creator of the PubSubHubbub protocol, and he launched Google’s first cloud computing product (App Engine). Fourteen years ago, he cut his teeth using Python to manage Google’s enormous fleet of servers. Outside of his day job, he likes to play piano and surf (both poorly). He also enjoys writing about programming-related topics on his personal website (https://onebigfluke.com). He earned his B.S. in computer engineering from Columbia University in the City of New York. He lives in San Francisco.Review “I have been recommending this book enthusiastically since the first edition appeared in 2015. This new edition, updated and expanded for Python 3, is a treasure trove of practical Python programming wisdom that can benefit programmers of all experience levels.”
–Wes McKinney, Creator of Python Pandas project, Director of Ursa Labs
“If you’re coming from another language, this is your definitive guide to taking full advantage of the unique features Python has to offer. I’ve been working with Python for nearly twenty years and I still learned a bunch of useful tricks, especially around newer features introduced by Python 3. Effective Python is crammed with actionable advice, and really helps define what our community means when they talk about Pythonic code.”
–Simon Willison, Co-creator of Django
“I’ve been programming in Python for years and thought I knew it pretty well. Thanks to this treasure trove of tips and techniques, I’ve discovered many ways to improve my Python code to make it faster (e.g., using bisect to search sorted lists), easier to read (e.g., enforcing keyword-only arguments), less prone to error (e.g., unpacking with starred expressions), and more Pythonic (e.g., using zip to iterate over lists in parallel). Plus, the second edition is a great way to quickly get up to speed on Python 3 features, such as the walrus operator, f-strings, and the typing module.”
–Pamela Fox, Creator of Khan Academy programming courses
“Now that Python 3 has finally become the standard version of Python, it’s already gone through eight minor releases and a lot of new features have been added throughout. Brett Slatkin returns with a second edition of Effective Python with a huge new list of Python idioms and straightforward recommendations, catching up with everything that’s introduced in version 3 all the way through 3.8 that we’ll all want to use as we finally leave Python 2 behind. Early sections lay out an enormous list of tips regarding new Python 3 syntaxes and concepts like string and byte objects, f-strings, assignment expressions (and their special nickname you might not know), and catch-all unpacking of tuples. Later sections take on bigger subjects, all of which are packed with things I either didn’t know or which I’m always trying to teach to others, including ‘Metaclasses and Attributes’ (good advice includes ‘Prefer Class Decorators over Metaclasses’ and also introduces a new magic method ‘__init_subclass__()’ I wasn’t familiar with), ‘Concurrency’ (favorite advice: ‘Use Threads for Blocking I/O, but not Parallelism,’ but it also covers asyncio and coroutines correctly) and ‘Robustness and Performance’ (advice given: ‘Profile before Optimizing’). It’s a joy to go through each section as everything I read is terrific best practice information smartly stated, and I’m considering quoting from this book in the future as it has such great advice all throughout. This is the definite winner for the ‘if you only read one Python book this year…’ contest.”
–Mike Bayer, Creator of SQLAlchemy
“This is a great book for both novice and experienced programmers. The code examples and explanations are well thought out and explained concisely and thoroughly. The second edition updates the advice for Python 3, and it’s fantastic! I’ve been using Python for almost 20 years, and I learned something new every few pages. The advice given in this book will serve anyone well.”
–Titus Brown, Associate Professor at UC Davis
“Once again, Brett Slatkin has managed to condense a wide range of solid practices from the community into a single volume. From exotic topics like metaclasses and concurrency to crucial basics like robustness, testing, and collaboration, the updated Effective Python makes a consensus view of what’s ‘Pythonic’ available to a wide audience.”
–Brandon Rhodes, Author of python-patterns.guide
Book details
- Print Length: 480 pages
- Publisher: Addison-Wesley Professional; 2nd Edition (October 25, 2019)
- Publication Date : October 25, 2019
- Simultaneous Device Usage: Up to 5 simultaneous devices, per publisher limits - See De-DRM from greedy publishers. Once downloaded you can't reset the count after having deleted it from the device. This is bad, when we live in an era of frequent mobile or laptop device upgrades and replacements.
- Page Numbers Source ISBN: 0134853989
Contents at a Glance
- Acknowledgments
- About the Author
- Chapter 1: Pythonic Thinking
- Chapter 2: Python Lists and Python Dictionaries
- Chapter 3: Python Functions
- Chapter 4: Python Comprehensions and Python Generators
- Chapter 5: Python Classes and Python Interfaces
- Chapter 6: Python Metaclasses and Python Attributes
- Chapter 7: Python Concurrency and Python Parallelism
- Chapter 8: Python Robustness and Python Performance
- Chapter 9: Python Testing and Python Debugging
- Chapter 10: Python Collaboration
Detailed Table of Contents
- Acknowledgments
- About the Author
Chapter 1 Pythonic Thinking
Chapter 1 Pythonic Thinking
- Item 1: Know Which Version of Python You’re Using
- Item 2: Follow the PEP 8 Style Guide
- Item 3: Know the Differences Between bytes and str
- Item 5: Write Helper Functions Instead of Complex Expressions]]
- Item 7: Prefer enumerate Over range
Chapter 2 Lists and Dictionaries
Chapter 2 Lists and Dictionaries
- Item 11: Know How to Slice Sequences
Chapter 3 Functions
Chapter 3 Python Functions
Chapter 4 Comprehensions and Generators
Chapter 4 Python Comprehensions and Generators
- Item 27: Use Comprehensions Instead of map and filter
]]
Item 28: Avoid More Than Two Control Subexpressions in Comprehensions
Item 29: Avoid Repeated Work in Comprehensions by Using Assignment Expressions
Item 30: Consider Generators Instead of Returning Lists
Item 31: Be Defensive When Iterating Over Arguments
Item 32: Consider Generator Expressions for Large List Comprehensions
Item 33: Compose Multiple Generators with yield from
Item 34: Avoid Injecting Data into Generators with send
Item 35: Avoid Causing State Transitions in Generators with throw
Item 36: Consider itertools for Working with Iterators and Generators
Chapter 4 Classes and Interfaces
Chapter 5 Classes and Interfaces
Item 37: Compose Classes Instead of Nesting Many Levels of Built-in Types
Item 38: Accept Functions Instead of Classes for Simple Interfaces
Item 39: Use @classmethod Polymorphism to Construct Objects Generically
Item 40: Initialize Parent Classes with super
Item 41: Consider Composing Functionality with Mix-in Classes
Item 42: Prefer Public Attributes Over Private Ones
Item 43: Inherit from collections.abc for Custom Container Types
Chapter 6 Metaclasses and Attributes
Chapter 6 Metaclasses and Attributes
Item 44: Use Plain Attributes Instead of Setter and Getter Methods
Item 45: Consider @property Instead of Refactoring Attributes
Item 46: Use Descriptors for Reusable @property Methods
Item 47: Use __getattr__, __getattribute__, and __setattr__ for Lazy Attributes
Item 48: Validate Subclasses with __init_subclass__
Item 49: Register Class Existence with __init_subclass__
Item 50: Annotate Class Attributes with __set_name__
Item 51: Prefer Class Decorators Over Metaclasses for Composable Class Extensions
Chapter 7 Concurrency and Parallelism
Chapter 7 Concurrency and Parallelism
Item 52: Use subprocess to Manage Child Processes
Item 53: Use Threads for Blocking I/O, Avoid for Parallelism
Item 54: Use Lock to Prevent Data Races in Threads
Item 55: Use Queue to Coordinate Work Between Threads
Item 56: Know How to Recognize When Concurrency Is Necessary
Item 57: Avoid Creating New Thread Instances for On-demand Fan-out
Item 58: Understand How Using Queue for Concurrency Requires Refactoring
Item 59: Consider ThreadPoolExecutor When Threads Are Necessary for Concurrency
Item 60: Achieve Highly Concurrent I/O with Coroutines
Item 61: Know How to Port Threaded I/O to asyncio
Item 62: Mix Threads and Coroutines to Ease the Transition to asyncio
Item 63: Avoid Blocking the asyncio Event Loop to Maximize Responsiveness
Item 64: Consider concurrent.futures for True Parallelism
Chapter 8 Robustness and Performance
Chapter 8 Robustness and Performance
Item 65: Take Advantage of Each Block in try/except/else/finally
Item 66: Consider contextlib and with Statements for Reusable try/finally Behavior
Item 67: Use datetime Instead of time for Local Clocks
Item 68: Make pickle Reliable with copyreg
Item 69: Use decimal When Precision Is Paramount
Item 70: Profile Before Optimizing
Item 71: Prefer deque for Producer–Consumer Queues
Item 72: Consider Searching Sorted Sequences with bisect
Item 73: Know How to Use heapq for Priority Queues
Item 74: Consider memoryview and bytearray for Zero-Copy Interactions with bytes
Chapter 9 Testing and Debugging
Chapter 9 Testing and Debugging
Item 75: Use repr Strings for Debugging Output
Item 76: Verify Related Behaviors in TestCase Subclasses
Item 77: Isolate Tests from Each Other with setUp, tearDown, setUpModule, and tearDownModule
Item 78: Use Mocks to Test Code with Complex Dependencies
Item 79: Encapsulate Dependencies to Facilitate Mocking and Testing
Item 80: Consider Interactive Debugging with pdb
Item 81: Use tracemalloc to Understand Memory Usage and Leaks
Chapter 10 Collaboration
Chapter 10 Python Collaboration
Item 82: Know Where to Find Community-Built Modules
Item 83: Use Virtual Environments for Isolated and Reproducible Dependencies
Item 84: Write Docstrings for Every Function, Class, and Module
Item 85: Use Packages to Organize Modules and Provide Stable APIs
Item 86: Consider Module-Scoped Code to Configure Deployment Environments
Item 87: Define a Root Exception to Insulate Callers from APIs
Item 88: Know How to Break Circular Dependencies
Item 89: Consider warnings to Refactor and Migrate Usage
Item 90: Consider Static Analysis via typing to Obviate Bugs
Index