Contents

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