Contents
Cover Page
About This eBook
Halftitle Page
Title Page
Copyright Page
Dedication Page
Contents at a Glance
Contents
Preface
What This Book Covers
Conventions Used in This Book
Acknowledgments
About the Author
Chapter 1. Pythonic Thinking
Item 1: Know Which Version of Python You’re Using
Item 2: Follow the PEP 8 Style Guide
Item 3: Never Expect Python to Detect Errors at Compile Time
Item 4: Write Helper Functions Instead of Complex Expressions
Item 5: Prefer Multiple-Assignment Unpacking over Indexing
Item 6: Always Surround Single-Element Tuples with Parentheses
Item 7: Consider Conditional Expressions for Simple Inline Logic
Item 8: Prevent Repetition with Assignment Expressions
Item 9: Consider match for Destructuring in Flow Control; Avoid When if Statements Are Sufficient
Chapter 2. Strings and Slicing
Item 10: Know the Differences Between bytes and str
Item 11: Prefer Interpolated F-Strings over C-Style Format Strings and str.format
Item 12: Understand the Difference Between repr and str when Printing Objects
Item 13: Prefer Explicit String Concatenation over Implicit, Especially in Lists
Item 14: Know How to Slice Sequences
Item 15: Avoid Striding and Slicing in a Single Expression
Item 16: Prefer Catch-All Unpacking over Slicing
Chapter 3. Loops and Iterators
Item 17: Prefer enumerate over range
Item 18: Use zip to Process Iterators in Parallel
Item 19: Avoid else Blocks After for and while Loops
Item 20: Never Use for Loop Variables After the Loop Ends
Item 21: Be Defensive when Iterating over Arguments
Item 22: Never Modify Containers While Iterating over Them; Use Copies or Caches Instead
Item 23: Pass Iterators to any and all for Efficient Short-Circuiting Logic
Item 24: Consider itertools for Working with Iterators and Generators
Chapter 4. Dictionaries
Item 25: Be Cautious when Relying on Dictionary Insertion Ordering
Item 26: Prefer get over in and KeyError to Handle Missing Dictionary Keys
Item 27: Prefer defaultdict over setdefault to Handle Missing Items in Internal State
Item 28: Know How to Construct Key-Dependent Default Values with __missing__
Item 29: Compose Classes Instead of Deeply Nesting Dictionaries, Lists, and Tuples
Chapter 5. Functions
Item 30: Know That Function Arguments Can Be Mutated
Item 31: Return Dedicated Result Objects Instead of Requiring Function Callers to Unpack More Than Three Variables
Item 32: Prefer Raising Exceptions to Returning None
Item 33: Know How Closures Interact with Variable Scope and nonlocal
Item 34: Reduce Visual Noise with Variable Positional Arguments
Item 35: Provide Optional Behavior with Keyword Arguments
Item 36: Use None and Docstrings to Specify Dynamic Default Arguments
Item 37: Enforce Clarity with Keyword-Only and Positional-Only Arguments
Item 38: Define Function Decorators with functools.wraps
Item 39: Prefer functools.partial over lambda Expressions for Glue Functions
Chapter 6. Comprehensions and Generators
Item 40: Use Comprehensions Instead of map and filter
Item 41: Avoid More Than Two Control Subexpressions in Comprehensions
Item 42: Reduce Repetition in Comprehensions with Assignment Expressions
Item 43: Consider Generators Instead of Returning Lists
Item 44: Consider Generator Expressions for Large List Comprehensions
Item 45: Compose Multiple Generators with yield from
Item 46: Pass Iterators into Generators as Arguments Instead of Calling the send Method
Item 47: Manage Iterative State Transitions with a Class Instead of the Generator throw Method
Chapter 7. Classes and Interfaces
Item 48: Accept Functions Instead of Classes for Simple Interfaces
Item 49: Prefer Object-Oriented Polymorphism over Functions with isinstance Checks
Item 50: Consider functools.singledispatch for Functional-Style Programming Instead of Object-Oriented Polymorphism
Item 51: Prefer dataclasses for Defining Lightweight Classes
Item 52: Use @classmethod Polymorphism to Construct Objects Generically
Item 53: Initialize Parent Classes with super
Item 54: Consider Composing Functionality with Mix-in Classes
Item 55: Prefer Public Attributes over Private Ones
Item 56: Prefer dataclasses for Creating Immutable Objects
Item 57: Inherit from collections.abc Classes for Custom Container Types
Chapter 8. Metaclasses and Attributes
Item 58: Use Plain Attributes Instead of Setter and Getter Methods
Item 59: Consider @property Instead of Refactoring Attributes
Item 60: Use Descriptors for Reusable @property Methods
Item 61: Use __getattr__, __getattribute__, and __setattr__ for Lazy Attributes
Item 62: Validate Subclasses with __init_subclass__
Item 63: Register Class Existence with __init_subclass__
Item 64: Annotate Class Attributes with __set_name__
Item 65: Consider Class Body Definition Order to Establish Relationships Between Attributes
Item 66: Prefer Class Decorators over Metaclasses for Composable Class Extensions
Chapter 9. Concurrency and Parallelism
Item 67: Use subprocess to Manage Child Processes
Item 68: Use Threads for Blocking I/O; Avoid for Parallelism
Item 69: Use Lock to Prevent Data Races in Threads
Item 70: Use Queue to Coordinate Work Between Threads
Item 71: Know How to Recognize When Concurrency Is Necessary
Item 72: Avoid Creating New Thread Instances for On-Demand Fan-out
Item 73: Understand How Using Queue for Concurrency Requires Refactoring
Item 74: Consider ThreadPoolExecutor When Threads Are Necessary for Concurrency
Item 75: Achieve Highly Concurrent I/O with Coroutines
Item 76: Know How to Port Threaded I/O to asyncio
Item 77: Mix Threads and Coroutines to Ease the Transition to asyncio
Item 78: Maximize Responsiveness of asyncio Event Loops with async-Friendly Worker Threads
Item 79: Consider concurrent.futures for True Parallelism
Chapter 10. Robustness
Item 80: Take Advantage of Each Block in try/except/else/finally
Item 81: assert Internal Assumptions and raise Missed Expectations
Item 82: Consider contextlib and with Statements for Reusable try/finally Behavior
Item 83: Always Make try Blocks as Short as Possible
Item 84: Beware of Exception Variables Disappearing
Item 85: Beware of Catching the Exception Class
Item 86: Understand the Difference Between Exception and BaseException
Item 87: Use traceback for Enhanced Exception Reporting
Item 88: Consider Explicitly Chaining Exceptions to Clarify Tracebacks
Item 89: Always Pass Resources into Generators and Have Callers Clean Them Up Outside
Item 90: Never Set __debug__ to False
Item 91: Avoid exec and eval Unless You’re Building a Developer Tool
Chapter 11. Performance
Item 92: Profile Before Optimizing
Item 93: Optimize Performance-Critical Code Using timeit Microbenchmarks
Item 94: Know When and How to Replace Python with Another Programming Language
Item 95: Consider ctypes to Rapidly Integrate with Native Libraries
Item 96: Consider Extension Modules to Maximize Performance and Ergonomics
Item 97: Rely on Precompiled Bytecode and File System Caching to Improve Startup Time
Item 98: Lazy-Load Modules with Dynamic Imports to Reduce Startup Time
Item 99: Consider memoryview and bytearray for Zero-Copy Interactions with bytes
Chapter 12. Data Structures and Algorithms
Item 100: Sort by Complex Criteria Using the key Parameter
Item 101: Know the Difference Between sort and sorted
Item 102: Consider Searching Sorted Sequences with bisect
Item 103: Prefer deque for Producer–Consumer Queues
Item 104: Know How to Use heapq for Priority Queues
Item 105: Use datetime Instead of time for Local Clocks
Item 106: Use decimal when Precision Is Paramount
Item 107: Make pickle Serialization Maintainable with copyreg
Chapter 13. Testing and Debugging
Item 108: Verify Related Behaviors in TestCase Subclasses
Item 109: Prefer Integration Tests over Unit Tests
Item 110: Isolate Tests from Each Other with setUp, tearDown, setUpModule, and tearDownModule
Item 111: Use Mocks to Test Code with Complex Dependencies
Item 112: Encapsulate Dependencies to Facilitate Mocking and Testing
Item 113: Use assertAlmostEqual to Control Precision in Floating Point Tests
Item 114: Consider Interactive Debugging with pdb
Item 115: Use tracemalloc to Understand Memory Usage and Leaks
Chapter 14. Collaboration
Item 116: Know Where to Find Community-Built Modules
Item 117: Use Virtual Environments for Isolated and Reproducible Dependencies
Item 118: Write Docstrings for Every Function, Class, and Module
Item 119: Use Packages to Organize Modules and Provide Stable APIs
Item 120: Consider Module-Scoped Code to Configure Deployment Environments
Item 121: Define a Root Exception to Insulate Callers from APIs
Item 122: Know How to Break Circular Dependencies
Item 123: Consider warnings to Refactor and Migrate Usage
Item 124: Consider Static Analysis via typing to Obviate Bugs
Item 125: Prefer Open Source Projects for Bundling Python Programs over zipimport and zipapp
Index
Code Snippets
i
ii
iii
iv
v
vi
vii
viii
ix
x
xi
xii
xiii
xiv
xv
xvi
xvii
xviii
xix
xx
xxi
xxii
xxiii
xxiv
xxv
xxvi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646