Contents
1 Introduction 11
1.1 What’s a Python Trick? . . . . . . . . . . . . . . . . 11
1.2 What This Book Will Do for You . . . . . . . . . . . 13
1.3 How to Read This Book . . . . . . . . . . . . . . . . 14
2 Patterns for Cleaner Python 15
2.1 Covering Your A** With Assertions . . . . . . . . . . 16
2.2 Complacent Comma Placement . . . . . . . . . . . . 25
2.3 Context Managers and the with Statement . . . . . . 29
2.4 Underscores, Dunders, and More . . . . . . . . . . . 36
2.5 A Shocking Truth About String Formatting . . . . . . 48
2.6 “The Zen of Python” Easter Egg . . . . . . . . . . . . 56
3 Effective Functions 57
3.1 Python’s Functions Are First-Class . . . . . . . . . . 58
3.2 Lambdas Are Single-Expression Functions . . . . . . 68
3.3 The Power of Decorators . . . . . . . . . . . . . . . 73
3.4 Fun With *args and **kwargs . . . . . . . . . . . . 86
3.5 Function Argument Unpacking . . . . . . . . . . . . 91
3.6 Nothing to Return Here . . . . . . . . . . . . . . . . 94
4 Classes & OOP 97
4.1 Object Comparisons: “is” vs “==” . . . . . . . . . . . 98
4.2 String Conversion (Every Class Needs a __repr__) . 101
4.3 Defining Your Own Exception Classes . . . . . . . . 111
4.4 Cloning Objects for Fun and Profit . . . . . . . . . . 116
4.5 Abstract Base Classes Keep Inheritance in Check . . . 124
4.6 What Namedtuples Are Good For . . . . . . . . . . . 128
4.7 Class vs Instance Variable Pitfalls . . . . . . . . . . . 136
4.8 Instance, Class, and Static Methods Demystified . . . 143
5 Common Data Structures in Python 153
5.1 Dictionaries, Maps, and Hashtables . . . . . . . . . 156
5.2 Array Data Structures . . . . . . . . . . . . . . . . . 163
5.3 Records, Structs, and Data Transfer Objects . . . . . 173
5.4 Sets and Multisets . . . . . . . . . . . . . . . . . . . 185
5.5 Stacks (LIFOs) . . . . . . . . . . . . . . . . . . . . 189
5.6 Queues (FIFOs) . . . . . . . . . . . . . . . . . . . . 195
5.7 Priority Queues . . . . . . . . . . . . . . . . . . . . 201
6 Looping & Iteration 205
6.1 Writing Pythonic Loops . . . . . . . . . . . . . . . . 206
6.2 Comprehending Comprehensions . . . . . . . . . . 210
6.3 List Slicing Tricks and the Sushi Operator . . . . . . 214
6.4 Beautiful Iterators . . . . . . . . . . . . . . . . . . . 218
6.5 Generators Are Simplified Iterators . . . . . . . . . 231
6.6 Generator Expressions . . . . . . . . . . . . . . . . 239
6.7 Iterator Chains . . . . . . . . . . . . . . . . . . . . 246
7 Dictionary Tricks 250
7.1 Dictionary Default Values . . . . . . . . . . . . . . . 251
7.2 Sorting Dictionaries for Fun and Profit . . . . . . . . 255
7.3 Emulating Switch/Case Statements With Dicts . . . . 259
7.4 The Craziest Dict Expression in the West . . . . . . . 264
7.5 So Many Ways to Merge Dictionaries . . . . . . . . . 271
7.6 Dictionary Pretty-Printing . . . . . . . . . . . . . . 274
Pythonic Productivity Techniques 277
8.1 Exploring Python Modules and Objects . . . . . . . . 278
8.2 Isolating Project Dependencies With Virtualenv . . . 282
8.3 Peeking Behind the Bytecode Curtain . . . . . . . . . 288
9 Closing Thoughts 293
9.1 Free Weekly Tips for Python Developers . . . . . . . 295
9.2 PythonistaCafe: A Community for Python Developers 296