Description
Book SynopsisDavid Mertz has been a member of the Python community for 25 years. He has taught Python to scientists, developers coming from other languages, and to programming neophytes. He was a director of the Python Software Foundation (PSF) for six years and continues to serve on, or chair, a variety of PSF working groups. David is the author of several technical books and has given keynote addresses at numerous international programming conferences.
Trade Review"You'll not just be aspiring to be an expert anymore after practicing through Better Python Code: A Guide for Aspiring Experts, you'll be one of them! Learn from David Mertz, who's been making experts through his writing and training for the past 20 years."
--Iqbal Abdullah, past Chair, PyCon Asia Pacific, and past board member, PyCon Japan
"In Better Python Code: A Guide for Aspiring Experts, David Mertz serves up bite-sized chapters of Pythonic wisdom in this must-have addition to any serious Python programmer's collection. This book helps bridge the gap from beginner to advanced Python user, but even the most seasoned Python programmer can up their game with Mertz's insight into the ins and outs of Python."
--Katrina Riehl, President, NumFOCUS
"What separates ordinary coders from Python experts? It's more than just knowing best practices--it's understanding the benefits and pitfalls of the many aspects of Python, and knowing when and why to choose one approach over another. In this book David draws on his more than 20 years of involvement in the Python ecosystem and his experience as a Python author to make sure that the readers understand both what to do and why in a wide variety of scenarios."
--Naomi Ceder, past Chair, Python Software Foundation
"Like a Pythonic BBC, David Mertz has been informing, entertaining, and educating the Python world for over a quarter of a century, and he continues to do so here in his own pleasantly readable style."
--Steve Holden, past Chair, Python Software Foundation
"Being expert means someone with a lot of experience. David's latest book provides some important but common problems that folks generally learn only after spending years of doing and fixing. I think this book will provide a much quicker way to gather those important bits and help many folks across the world to become better."
--Kushal Das, CPython Core Developer and Director, Python Software Foundation
"This book is for everyone: from beginners, who want to avoid hard-to-find bugs, all the way to experts looking to write more efficient code. David Mertz has compiled a great set of useful idioms that will make your life as a programmer easier and your users happier."
--Marc-André Lemburg, past Chair, EuroPython, and past Director, Python Software Foundation
Table of ContentsForeword xvii
Preface xix
Acknowledgments xxv
About the Author xxvii
Introduction 1
Chapter 1: Looping Over the Wrong Things 3
1.1 (Rarely) Generate a List for Iteration 3
1.2 Use enumerate() Instead of Looping Over an Index 6
1.3 Don't Iterate Over dict.keys() When You Want dict.items() 8
1.4 Mutating an Object During Iteration 9
1.5 for Loops Are More Idiomatic Than while Loops 12
1.6 The Walrus Operator for "Loop-and-a-Half" Blocks 13
1.7 zip() Simplifies Using Multiple Iterables 15
1.8 zip(strict=True) and itertools.zip_longest() 17
1.9 Wrapping Up 20
Chapter 2: Confusing Equality with Identity 21
2.1 Late Binding of Closures 21
2.2 Overchecking for Boolean Values 25
2.3 Comparing x == None 28
2.4 Misunderstanding Mutable Default Arguments 29
2.5 Copies versus References to Mutable Objects 33
2.6 Confusing is with == (in the Presence of Interning) 35
2.7 Wrapping Up 37
Chapter 3: A Grab Bag of Python Gotchas 39
3.1 Naming Things 39
3.2 Quadratic Behavior of Naive String Concatenation 52
3.3 Use a Context Manager to Open a File 56
3.4 Optional Argument key to .sort() and sorted() 59
3.5 Use dict.get() for Uncertain Keys 62
3.6 Wrapping Up 64
Chapter 4: Advanced Python Usage 67
4.1 Comparing type(x) == type(y) 67
4.2 Naming Things (Revisited) 71
4.3 Keep Less-Used Features in Mind 79
4.4 Type Annotations Are Not Runtime Types 98
4.5 Wrapping Up 105
Chapter 5: Just Because You Can, It Doesn't Mean You Should... 107
5.1 Metaclasses 107
5.2 Monkeypatching 112
5.3 Getters and Setters 115
5.4 It's Easier to Ask for Forgiveness Than Permission 118
5.5 Structural Pattern Matching 121
5.6 Regular Expressions and Catastrophic Backtracking 123
5.7 Wrapping Up 126
Chapter 6: Picking the Right Data Structure 129
6.1 collections.defaultdict 129
6.2 collections.Counter 132
6.3 collections.deque 135
6.4 collections.ChainMap 138
6.5 Dataclasses and Namedtuples 141
6.6 Efficient Concrete Sequences 146
6.7 Wrapping Up 150
Chapter 7: Misusing Data Structures 153
7.1 Quadratic Behavior of Repeated List Search 153
7.2 Deleting or Adding Elements to the Middle of a List 157
7.3 Strings Are Iterables of Strings 163
7.4 (Often) Use enum Rather Than CONSTANT 166
7.5 Learn Less Common Dictionary Methods 169
7.6 JSON Does Not Round-Trip Cleanly to Python 174
7.7 Rolling Your Own Data Structures 178
7.8 Wrapping Up 187
Chapter 8: Security 189
8.1 Kinds of Randomness 190
8.2 Putting Passwords or Other Secrets in "Secure" Source Code 195
8.3 "Rolling Your Own" Security Mechanisms 198
8.4 Use SSL/TLS for Microservices 201
8.5 Using the Third-Party requests Library 205
8.6 SQL Injection Attacks When Not Using DB-API 208
8.7 Don’t Use assert to Check Safety Assumptions 212
8.8 Wrapping Up 215
Chapter 9: Numeric Computation in Python 217
9.1 Understanding IEEE-754 Floating Point Numbers 217
9.2 Numeric Datatypes 228
9.3 Wrapping Up 239
Appendix: Topics for Other Books 241
A.1 Test-Driven Development 241
A.2 Concurrency 242
A.3 Packaging 243
A.4 Type Checking 243
A.5 Numeric and Dataframe Libraries 244
Index 245