Effective Python:編寫好Python的90個有效方法(第2版英文版)

《Effective Python:編寫好Python的90個有效方法(第2版 英文版)》是2024年人民郵電出版社出版的圖書。

基本介紹

  • 中文名:Effective Python:編寫好Python的90個有效方法(第2版 英文版)
  • 作者:[美] 布雷特·斯拉特金(Brett Slatkin)
  • 出版時間:2024年3月1日
  • 出版社:人民郵電出版社
  • 頁數:434 頁
  • 字數:612000
  • ISBN:9787115634061
  • 開本:16 開
內容簡介,圖書目錄,

內容簡介

本書是Effective Python的第2版,基於Python 3進行了全面升級。本書分為10章,包含90個條目,形式簡潔、表述明確。每個條目都基於對Python的獨到見解,告訴讀者如何高效地編寫Python程式。與第1版相比,第2版只關注Python 3,而不再兼顧 Python 2。根據Python語言引入的新特性,以及Python開發者所形成的新經驗,對第1版中的某些思路與解決方案進行了修訂,以更好地發揮Python的優勢。本書刪除了過時的知識點,並添加了對Python新特性的一些介紹。新版中增加了31個條目,並專門設立了幾章來強調列表和字典、推導和生成器、元類和屬性、穩定性和性能,以及測試和調試等重要主題。

圖書目錄

Chapter 1 Pythonic Thinking / 第 1 章 Pythonic 思維 1
Item 1: Know Which Version of Python You’re Using /
條目 1:查詢自己使用的 Python 版本 1
Item 2: Follow the PEP 8 Style Guide /
條目 2:遵循 PEP 8 風格指南 2
Item 3: Know the Differences Between bytes and str /
條目 3:了解 bytes 和 str 之間的區別 5
Item 4: Prefer Interpolated F-Strings Over C-style Format Strings and str.format /
條目 4:使用支持插值的 f-string 取代 C 風格的格式化字元串和str.format 11
Item 5: Write Helper Functions Instead of Complex Expressions /
條目 5:使用輔助函式取代複雜表達式 21
Item 6: Prefer Multiple Assignment Unpacking Over Indexing /
條目 6:把數據結構直接拆分到多個變數里,避免通過下標訪問 24
Item 7: Prefer enumerate Over range /
條目 7:儘量用 enumerate 取代 range 28
Item 8: Use zip to Process Iterators in Parallel /
條目 8:使用 zip 並行處理疊代器 30
Item 9: Avoid else Blocks After for and while Loops /
條目 9:避免在 for 和 while 循環後使用 else 塊 32
Item 10: Prevent Repetition with Assignment Expressions /
條目 10:使用賦值表達式減少重複代碼 35
Chapter 2 Lists and Dictionaries / 第 2 章 列表和字典 43
Item 11: Know How to Slice Sequences /
條目 11:學會對序列做切片 43
Item 12: Avoid Striding and Slicing in a Single Expression /
條目 12:避免在切片裡同時指定起止下標和步進 46
Item 13: Prefer Catch-All Unpacking Over Slicing /
條目 13:通過帶星號的 unpacking 操作來捕獲多個元素,避免用切片 48
Item 14: Sort by Complex Criteria Using the key Parameter /
條目 14:使用 sort 方法的 key 參數表示複雜的排序邏輯 52
Item 15: Be Cautious When Relying on dict Insertion Ordering /
條目 15:不要過分依賴給 dict 添加條目時所用的順序 58
Item 16: Prefer get Over in and KeyError to Handle Missing Dictionary Keys /
條目 16:使用 get 處理鍵不在字典中的情況,避免使用 in 與 KeyError 65
Item 17: Prefer defaultdict Over setdefault to Handle Missing Items in Internal State /
條目 17:使用 defaultdict 處理內部狀態中缺失的元素,而避免使用setdefault 70
Item 18: Know How to Construct Key-Dependent Default Values with __missing__ /
條目 18:學會使用__missing__構建依賴鍵的默認值 73
Chapter 3 Functions / 第 3 章 函式 77
Item 19: Never Unpack More Than Three Variables When Functions Retur Multiple Values /
條目 19:避免把函式返回的多個數值拆分到 3 個以上的變數中 77
Item 20: Prefer Raising Exceptions to Returning None /
條目 20:遇到意外狀況時應該拋出異常,而不是返回 None 80
Item 21: Know How Closures Interact with Variable Scope /
條目 21:了解如何在閉包裡面使用外圍作用域中的變數 83
Item 22: Reduce Visual Noise with Variable Positional Arguments /
條目 22:使用數量可變的位置參數給函式設計清晰的參數列表 87
Item 23: Provide Optional Behavior with Keyword Arguments /
條目 23:使用關鍵字參數表示可選行為 90
Item 24: Use None and Docstrings to Specify Dynamic Default Arguments /
條目 24:使用 None 和文檔字元串描述默認值會變的參數 94
Item 25: Enforce Clarity with Keyword-Only and Positional-Only Arguments /
條目 25:使用只能以關鍵字指定和只能按位置傳入的參數來設計清晰的參數列表 97
Item 26: Define Function Decorators with functools.wraps /
條目 26:使用 functools.wraps 定義函式修飾器 102
Chapter 4 Comprehensions and Generators / 第 4 章 推導和生成器 107
Item 27: Use Comprehensions Instead of map and filter /
條目 27:使用推導取代 map 和 filter 107
Item 28: Avoid More Than Two Control Subexpressions in Comprehensions /
條目 28:在推導中避免超過兩個控制子表達式 109
Item 29: Avoid Repeated Work in Comprehensions by Using Assignment Expressions /
條目 29:使用賦值表達式在推導中避免重複代碼 111
Item 30: Consider Generators Instead of Returning Lists /
條目 30:考慮使用生成器而不是返回列表 114
Item 31: Be Defensive When Iterating Over Arguments /
條目 31:謹慎地疊代函式所收到的參數 117
Item 32: Consider Generator Expressions for Large List Comprehensions /
條目 32:考慮使用生成器表達式來進行大型列表推導的組合 122
Item 33: Compose Multiple Generators with yield from /
條目 33:使用 yield from 組合多個生成器 124
Item 34: Avoid Injecting Data into Generators with send /
條目 34:避免使用 send 向生成器注入數據 127
Item 35: Avoid Causing State Transitions in Generators with throw /
條目 35:避免通過 throw 變換生成器的狀態 133
Item 36: Consider itertools for Working with Iterators and Generators /
條目 36:考慮使用 itertools 處理疊代器和生成器 138
Chapter 5 Classes and Interfaces / 第 5 章 類和接口 145
Item 37: Compose Classes Instead of Nesting Many Levels of Built-in Types /
條目 37:使用組合起來的類來實現多層結構,避免用嵌套的內置類型 145
Item 38: Accept Functions Instead of Classes for Simple Interfaces /
條目 38:接受函式而不是類來實現簡單接口 152
Item 39: Use @classmethod Polymorphism to Construct Objects Generically /
條目 39:通過@classmethod 多態來構建同一體系中的各類對象 155
Item 40: Initialize Parent Classes with super /
條目 40:使用 super 初始化超類 160
Item 41: Consider Composing Functionality with Mix-in Classes /
條目 41:考慮使用混合類來組合功能 165
Item 42: Prefer Public Attributes Over Private Ones /
條目 42:優先考慮使用共有屬性表示應受保護的數據,避免使用私有屬性表示 170
Item 43: Inherit from collections.abc for Custom Container Types /
條目 43:使用 collections.abc 繼承自定義容器類型 175
Chapter 6 Metaclasses and Attributes / 第 6 章 元類和屬性 181
Item 44: Use Plain Attributes Instead of Setter and Getter Methods /
條目 44:使用純屬性而不是 setter 和 getter 方法 181
Item 45: Consider @property Instead of Refactoring Attributes /
條目 45:考慮使用@property 而不是重構屬性 186
Item 46: Use Descriptors for Reusable @property Methods /
條目 46:使用描述符來改寫需要復用的@property 方法 190
Item 47: Use __getattr__, __getattribute__, and __setattr__ for Lazy Attributes /
條目 47:使用__getattr__、__getattribute__和__setattr__處理惰性屬性 195
Item 48: Validate Subclasses with __init_subclass__ /
條目 48:使用__init_subclass__驗證子類 201
Item 49: Register Class Existence with __init_subclass__ /
條目 49:使用__init_subclass__記錄現有的子類 208
Item 50: Annotate Class Attributes with __set_name__ /
條目 50:使用__set_name__注釋類屬性 214
Item 51: Prefer Class Decorators Over Metaclasses for Composable Class Extensions /
條目 51:使用類修飾器而不是元類來實現可組合的類擴展 218
Chapter 7 Concurrency and Parallelism / 第 7 章 並發和並行 225
Item 52: Use subprocess to Manage Child Processes /
條目 52:使用 subprocess 管理子進程 226
Item 53: Use Threads for Blocking I/O, Avoid for Parallelism /
條目 53:使用執行緒處理阻塞 I/O,但避免使用它做並行計算 230
Item 54: Use Lock to Prevent Data Races in Threads /
條目 54:使用 Lock 避免執行緒中的數據競爭 235
Item 55: Use Queue to Coordinate Work Between Threads /
條目 55:使用 Queue 協調執行緒間的工作 238
Item 56: Know How to Recognize When Concurrency Is Necessary /
條目 56:學會判斷何時需要並發 248
Item 57: Avoid Creating New Thread Instances for On-demand Fan-out /
條目 57:避免為按需分發創建新的 Thread 實例 252
Item 58: Understand How Using Queue for Concurrency Requires Refactoring /
條目 58:學會正確地重構代碼,以便用 Queue 做並發 257
Item 59: Consider ThreadPoolExecutor When Threads Are Necessary for Concurrency /
條目 59:在需要並發時考慮 ThreadPoolExecutor 264
Item 60: Achieve Highly Concurrent I/O with Coroutines /
條目 60:使用協程實現高並發 I/O 266
Item 61: Know How to Port Threaded I/O to asyncio /
條目 61:了解如何將基於執行緒的 I/O 移植到 asyncio 271
Item 62: Mix Threads and Coroutines to Ease the Transition to asyncio /
條目 62:混合使用執行緒和協程以便過渡到 asyncio 282
Item 63: Avoid Blocking the asyncio Event Loop to Maximize Responsiveness /
條目 63:避免阻塞 asyncio 事件循環以最大化程式的回響能力 289
Item 64: Consider concurrent.futures for True Parallelism /
條目 64:考慮 concurrent.futures 以實現真正的並行計算 292
Chapter 8 Robustness and Performance / 第 8 章 穩定性和性能 299
Item 65: Take Advantage of Each Block in try/except/else/finally /
條目 65:充分利用 try/except/else/finally 結構中的每個代碼塊 299
Item 66: Consider contextlib and with Statements for Reusable try/finally Behavior /
條目 66:考慮使用 contextlib 和 with 語句來改寫可復用的 try/finally 代碼 304
Item 67: Use datetime Instead of time for Local Clocks /
條目 67:使用 datetime 模組而不是 time 模組處理本地時間 308
Item 68: Make pickle Reliable with copyreg /
條目 68:使用 copyreg 實現可靠的 pickle 操作 312
Item 69: Use decimal When Precision Is Paramount /
條目 69:在需要準確計算時使用 decimal 表示相應的數值 319
Item 70: Profile Before Optimizing /
條目 70:在最佳化之前進行性能分析 322
Item 71: Prefer deque for Producer – Consumer Queues /
條目 71:優先考慮使用 deque 實現生產者-消費者佇列 326
Item 72: Consider Searching Sorted Sequences with bisect /
條目 72:考慮使用 bisect 搜尋排序序列 334
Item 73: Know How to Use heapq for Priority Queues /
條目 73:學會使用 heapq 製作優先權佇列 336
Item 74: Consider memoryview and bytearray for Zero-Copy Interactions with bytes /
條目 74:考慮使用 memoryview 和 bytearray 來實現無須拷貝的 bytes 操作 346
Chapter 9 Testing and Debugging / 第 9 章 測試和調試 353
Item 75: Use repr Strings for Debugging Output /
條目 75:使用 repr 字元串輸出調試信息 354
Item 76: Verify Related Behaviors in TestCase Subclasses /
條目 76:通過 TestCase 子類驗證相關行為 357
Item 77: Isolate Tests from Each Other with setUp, tearDown, setUpModule, and tearDownModule /
條目 77:使用 setUp、tearDown、setUpModule 和tearDownModule 將測試隔離開 365

相關詞條

熱門詞條

聯絡我們