<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ### 導航 - [索引](../genindex.xhtml "總目錄") - [模塊](../py-modindex.xhtml "Python 模塊索引") | - [下一頁](3.6.xhtml "Python 3.6 有什么新變化A") | - [上一頁](index.xhtml "Python 有什么新變化?") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) ? - zh\_CN 3.7.3 [文檔](../index.xhtml) ? - [Python 有什么新變化?](index.xhtml) ? - $('.inline-search').show(0); | # Python 3.7 有什么新變化 編者Elvis Pranskevichus <[elvis@magic.io](mailto:elvis%40magic.io)> 本文解釋了 Python 3.7 相比 3.6 的新增特性。Python 3.7 于 2018 年 6 月 27 日發布。完整的詳情可參閱 [更新日志](changelog.xhtml#changelog)。 ## 摘要 - 發布重點 新的語法特性: - [PEP 563](#whatsnew37-pep563),類型標注延遲求值。 向后不兼容的語法更改: - [`async`](../reference/compound_stmts.xhtml#async) 和 [`await`](../reference/expressions.xhtml#await) 現在是保留的關鍵字。 新的庫模塊: - [`contextvars`](../library/contextvars.xhtml#module-contextvars "contextvars: Context Variables"): [PEP 567 -- 上下文變量](#whatsnew37-pep567) - [`dataclasses`](../library/dataclasses.xhtml#module-dataclasses "dataclasses: Generate special methods on user-defined classes."): [PEP 557 -- 數據類](#whatsnew37-pep557) - [importlib.resources](#whatsnew37-importlib-resources) 新的內置特性: - [PEP 553](#whatsnew37-pep553), 新的 [`breakpoint()`](../library/functions.xhtml#breakpoint "breakpoint") 函數。 對 Python 數據模型的改進: - [PEP 562](#whatsnew37-pep562), 自定義可訪問的模塊屬性。 - [PEP 560](#whatsnew37-pep560), typing模塊和泛型類型的核心支持。 - [dict](../library/stdtypes.xhtml#typesmapping) 對象會保持插入時的順序這個特性 [正式宣布](https://mail.python.org/pipermail/python-dev/2017-December/151283.html) \[https://mail.python.org/pipermail/python-dev/2017-December/151283.html\] 成為 Python 語言官方規范的一部分。 標準庫中的重大改進: - [`asyncio`](../library/asyncio.xhtml#module-asyncio "asyncio: Asynchronous I/O.") 模塊添加了新的功能,重大改進請參閱 [可用性與性能提升](#whatsnew37-asyncio)。 - [`time`](../library/time.xhtml#module-time "time: Time access and conversions.") 模塊現在提供 [納秒級精度函數](#whatsnew37-pep564) 的支持。 CPython 實現的改進: - 避免使用 ASCII 作為默認的文本編碼: - [PEP 538](#whatsnew37-pep538),傳統 C 區域強制轉換 - [PEP 540](#whatsnew37-pep540),強制 UTF-8 運行時模式 - [PEP 552](#whatsnew37-pep552),確定性的 .pyc 文件 - [新的開發運行時模式](#whatsnew37-devmode) - [PEP 565](#whatsnew37-pep565),改進的 [`DeprecationWarning`](../library/exceptions.xhtml#DeprecationWarning "DeprecationWarning") 處理 C API 的改進: - [PEP 539](#whatsnew37-pep539),用于線程本地存儲的新 C API 文檔的改進: - [PEP 545](#whatsnew37-pep545), Python 文檔翻譯 - 新的文檔翻譯:[Japanese](https://docs.python.org/ja/) \[https://docs.python.org/ja/\],[French](https://docs.python.org/fr/) \[https://docs.python.org/fr/\] 和 [Korean](https://docs.python.org/ko/) \[https://docs.python.org/ko/\]。 此版本在諸多方面有顯著的性能改進。[性能優化](#whatsnew37-perf) 章節詳細列出了它們。 和之前的 Python 版本存在兼容性的更改列表,請參閱 [移植到 Python 3.7](#porting-to-python-37) 章節。 ## 新的特性 ### PEP 563:延遲的標注求值 隨著 [**PEP 3107**](https://www.python.org/dev/peps/pep-3107) \[https://www.python.org/dev/peps/pep-3107\] 加入標注功能并在 [**PEP 526**](https://www.python.org/dev/peps/pep-0526) \[https://www.python.org/dev/peps/pep-0526\] 進一步細化,Python 中類型提示的出現揭示了兩個明顯的可用性問題: - 標注只能使用在當前作用域中已經存在的名稱,也就是說,它們不支持任何形式的前向引用;而且—— - 標注源碼對 Python 程序的啟動時間有不利的影響。 這兩個問題都可以通過延遲標注求值來解決。在定義標注的時候,編譯器并不會編譯執行相應表達式的代碼,而是保存與相應表達式的 AST 等價的字符串形式。如果有需要,標注可以在運行時使用 [`typing.get_type_hints()`](../library/typing.xhtml#typing.get_type_hints "typing.get_type_hints") 進行解析。在不需要這種解析的通常情況下,標注的存儲成本更低(因為解析器只需處理較短的字符串)且啟動時間更短。 在可用性方面,標注現在支持向前引用,以使以下句法有效: ``` class C: @classmethod def from_string(cls, source: str) -> C: ... def validate_b(self, obj: B) -> bool: ... class B: ... ``` 由于此修改會破壞兼容性,在 Python 3.7 中此種新的行為需要在每個模塊層級上使用 [`__future__`](../library/__future__.xhtml#module-__future__ "__future__: Future statement definitions") 導入來啟用: ``` from __future__ import annotations ``` 它將在 Python 4.0 中成為默認行為。 參見 [**PEP 563**](https://www.python.org/dev/peps/pep-0563) \[https://www.python.org/dev/peps/pep-0563\] -- 延遲的標注求值PEP 由 ?ukasz Langa 撰寫并實現。 ### PEP 538: 傳統 C 區域強制轉換 Python 3 系列有一個持續的挑戰就是為處理 7 比特位 ASCII 文本的假定編碼確定合理的默認策略,目前的設定是在非 Windows 平臺上使用默認的 C 或 POSIX 區域設置。 [**PEP 538**](https://www.python.org/dev/peps/pep-0538) \[https://www.python.org/dev/peps/pep-0538\] 更新了默認的解釋器命令行接口,以自動將上述區域強制轉換為可用的基于 UTF-8 的區域,具體描述可參見有關新增環境變量 [`PYTHONCOERCECLOCALE`](../using/cmdline.xhtml#envvar-PYTHONCOERCECLOCALE) 的文檔。 以這種方式自動設置 `LC_CTYPE` 意味著核心解釋器和能感知區域的 C 擴展 (例如 [`readline`](../library/readline.xhtml#module-readline "readline: GNU readline support for Python. (Unix)")) 都將會假定 UTF-8 已被用作默認的文本編碼,而不再是 ASCII。 [**PEP 11**](https://www.python.org/dev/peps/pep-0011) \[https://www.python.org/dev/peps/pep-0011\] 中的平臺支持定義也已被更新以限制完整文本處理支持適當配置的基于非 ASCII 的語言區域。 作為此更改的一部分,當使用任何已定義的強制轉換目標區域時 (目前為 `C.UTF-8`, `C.utf8` 和 `UTF-8`) [`stdin`](../library/sys.xhtml#sys.stdin "sys.stdin") 和 [`stdout`](../library/sys.xhtml#sys.stdout "sys.stdout") 默認的處理器現在將為 `surrogateescape` (而不是 `strict`)。 而無論是什么區域,[`stderr`](../library/sys.xhtml#sys.stderr "sys.stderr") 默認的處理器仍為 `backslashreplace`。 默認情況下區域強制轉換會靜默進行,但為了輔助調試潛在的區域相關集成問題,可以通過設置 `PYTHONCOERCECLOCALE=warn` 來請求顯式地啟用警告信息(直接在 [`stderr`](../library/sys.xhtml#sys.stderr "sys.stderr") 上發出)。 此設置還會使得 Python 運行時在核心解釋器初始化時如果傳統 C 區域仍然處于激活狀態時發出警告。 雖然 [**PEP 538**](https://www.python.org/dev/peps/pep-0538) \[https://www.python.org/dev/peps/pep-0538\] 的區域強制轉換的好處在于它還會同時影響擴展模塊 (例如 GNU `readline`) 以及子進程 (包括運行非 Python 應用和舊版本 Python 的子進程),但它也存在需要所運行系統必須存在適合的目標區域的缺點。 為了更好地處理沒有可用適合的目標區域的情況 (例如在 RHEL/CentOS 7 上就會出現此情況),Python 3.7 還實現了 [PEP 540: 強制 UTF-8 運行時模式](#whatsnew37-pep540)。 參見 [**PEP 538**](https://www.python.org/dev/peps/pep-0538) \[https://www.python.org/dev/peps/pep-0538\] -- 強制轉換傳統 C 區域到基于 UTF-8 的區域PEP 由 Nick Coghlan 撰寫并實現。 ### PEP 540: 強制 UTF-8 運行時模式 新的 [`-X`](../using/cmdline.xhtml#id5)`utf8` 命令行選項和 [`PYTHONUTF8`](../using/cmdline.xhtml#envvar-PYTHONUTF8) 環境變量可被用來啟用 CPython 的 *UTF-8 模式*。 當處于 UTF-8 模式時,CPython 會忽略區域設置,并默認使用 UTF-8 編碼。 用于 [`sys.stdin`](../library/sys.xhtml#sys.stdin "sys.stdin") 和 [`sys.stdout`](../library/sys.xhtml#sys.stdout "sys.stdout") 流的錯誤處理器將設置為 `surrogateescape`。 強制 UTF-8 模式可被用來在嵌入的 Python 解釋器中改變文本處理行為,而不會改變嵌入方應用的區域設置。 [**PEP 540**](https://www.python.org/dev/peps/pep-0540) \[https://www.python.org/dev/peps/pep-0540\] 的 UTF-8 模式的好處是不必關心運行所在系統中有哪些可用區域即可工作,但它也存在對擴展模塊 (例如 GNU `readline`)、運行非 Python 應用的子進程以及運行舊版本 Python 的子進程不起作用的缺點。 為了減小與這些組件通信時破壞文本數據的風險,Python 3.7 還實現了 [PEP 540: 強制 UTF-8 運行時模式](#whatsnew37-pep540))。 UTF-8 模式在語言區域為 `C` 或 `POSIX` 并且 [**PEP 538**](https://www.python.org/dev/peps/pep-0538) \[https://www.python.org/dev/peps/pep-0538\] 區域強制轉換特性無法將其修改為某種基于 UTF-8 的替代項時會被默認啟用(無論修改失敗是由于設置了 `PYTHONCOERCECLOCALE=0`, `LC_ALL` 還是由于缺少適合的目標區域)。 參見 [**PEP 540**](https://www.python.org/dev/peps/pep-0540) \[https://www.python.org/dev/peps/pep-0540\] -- 增加了新的 UTF-8 模式PEP 由 Victor Stinner 撰寫并實現 ### PEP 553: 內置的 `breakpoint()` Python 3.7 包含了新的內置 [`breakpoint()`](../library/functions.xhtml#breakpoint "breakpoint") 函數,作為一種簡單方便地進入 Python 調試器的方式。 內置 `breakpoint()` 會調用 [`sys.breakpointhook()`](../library/sys.xhtml#sys.breakpointhook "sys.breakpointhook")。 在默認情況下后者會導入 [`pdb`](../library/pdb.xhtml#module-pdb "pdb: The Python debugger for interactive interpreters.") 然后再調用 `pdb.set_trace()`,但是通過將 `sys.breakpointhook()` 綁定到你選定的函數,`breakpoint()` 可以進入任何調試器。 此外,環境變量 [`PYTHONBREAKPOINT`](../using/cmdline.xhtml#envvar-PYTHONBREAKPOINT) 可被設置為你選定的調試器的可調用對象。 設置 `PYTHONBREAKPOINT=0` 會完全禁用內置 `breakpoint()`。 參見 [**PEP 553**](https://www.python.org/dev/peps/pep-0553) \[https://www.python.org/dev/peps/pep-0553\] -- 內置的 breakpoint()PEP 由 Barry Warsaw 撰寫并實現 ### PEP 539: 用于線程局部存儲的新 C API 雖然 Python 已提供了用于線程局部存儲支持的 C API;但原有的 [線程局部存儲 (TLS) API](../c-api/init.xhtml#thread-local-storage-api) 使用 `int` 來表示所有平臺上的 TLS 密鑰。 對于官方支持的平臺而言這通常不是問題,但這既不符合 POSIX 標準,也不具備任何實際意義上的可移植性。 [**PEP 539**](https://www.python.org/dev/peps/pep-0539) \[https://www.python.org/dev/peps/pep-0539\] 通過向 CPython 提供了一個新的 [線程特定存儲 (TSS) API](../c-api/init.xhtml#thread-specific-storage-api) 來改變這一點,它取代了原有的 CPython 內部 TLS API 的使用,并且原有 API 已棄用。 TSS API 使用一種新類型 [`Py_tss_t`](../c-api/init.xhtml#c.Py_tss_t "Py_tss_t") 而非 `int` 來表示 TSS 密鑰 -- 這是一種不透明類型,其定義可能依賴于下層的 TLS 實現。 因此,這將允許在以無法安全地轉換為 `int` 的方式定義原生 TLS 密鑰的平臺上構建 CPython。 請注意在原生 TLS 密鑰定義方式無法被安全地轉換為 `int` 的平臺上,原有 TLS API 中的全部函數將無法執行并會立即返回失敗信息。 這樣能清楚地表明原有 API 在無法可靠使用的平臺上不受支持,并且不會再嘗試添加此類支持。 參見 [**PEP 539**](https://www.python.org/dev/peps/pep-0539) \[https://www.python.org/dev/peps/pep-0539\] -- 在 CPython 中用于線程局部存儲的新 C-APIPEP 由 Erik M. Bray 撰寫;由 Masayuki Yamamoto 實現。 ### PEP 562: 定制對模塊屬性的訪問 Python 3.7 允許在模塊上定義 [`__getattr__()`](../reference/datamodel.xhtml#object.__getattr__ "object.__getattr__") 并且當以其他方式找不到某個模塊屬性時將會調用它。 在模塊上定義 [`__dir__()`](../reference/datamodel.xhtml#object.__dir__ "object.__dir__") 現在也是允許的。 一個典型的可能有用的例子是已棄用模塊屬性和惰性加載。 參見 [**PEP 562**](https://www.python.org/dev/peps/pep-0562) \[https://www.python.org/dev/peps/pep-0562\] -- 模塊的 `__getattr__` 和 `__dir__`PEP 由 Ivan Levkivskyi 撰寫并實現 ### PEP 564: 具有納秒級精度的新時間函數 現代系統的時鐘精度可以超過由 [`time.time()`](../library/time.xhtml#time.time "time.time") 函數及其變化形式所返回的浮點數的有限精度。 為了避免精度損失,[**PEP 564**](https://www.python.org/dev/peps/pep-0564) \[https://www.python.org/dev/peps/pep-0564\] 在 [`time`](../library/time.xhtml#module-time "time: Time access and conversions.") 模塊中增加了原有計時器函數的六個新“納秒版”變化形式: - [`time.clock_gettime_ns()`](../library/time.xhtml#time.clock_gettime_ns "time.clock_gettime_ns") - [`time.clock_settime_ns()`](../library/time.xhtml#time.clock_settime_ns "time.clock_settime_ns") - [`time.monotonic_ns()`](../library/time.xhtml#time.monotonic_ns "time.monotonic_ns") - [`time.perf_counter_ns()`](../library/time.xhtml#time.perf_counter_ns "time.perf_counter_ns") - [`time.process_time_ns()`](../library/time.xhtml#time.process_time_ns "time.process_time_ns") - [`time.time_ns()`](../library/time.xhtml#time.time_ns "time.time_ns") 這些新函數會以整數值的形式返回納秒數。 [測量](https://www.python.org/dev/peps/pep-0564/#annex-clocks-resolution-in-python) \[https://www.python.org/dev/peps/pep-0564/#annex-clocks-resolution-in-python\] 表明在 Linux 和 Windows 上 [`time.time_ns()`](../library/time.xhtml#time.time_ns "time.time_ns") 的精度大約比 [`time.time()`](../library/time.xhtml#time.time "time.time") 要高 3 倍。 參見 [**PEP 564**](https://www.python.org/dev/peps/pep-0564) \[https://www.python.org/dev/peps/pep-0564\] -- 增加具有納秒級精度的新時間函數PEP 由 Victor Stinner 撰寫并實現 ### PEP 565: 在 `__main__` 中顯示 DeprecationWarning [`DeprecationWarning`](../library/exceptions.xhtml#DeprecationWarning "DeprecationWarning") 的默認處理方式已經被更改,這此警告默認只顯示一次,僅有當直接在 [`__main__`](../library/__main__.xhtml#module-__main__ "__main__: The environment where the top-level script is run.") 模塊中運行的代碼觸發它們時才會再次顯示。 因此,單文件腳本開發者以及 Python 交互模式使用者應該會再次開始看到針對他們所使用 API 的已棄用警告,但被導入應用、庫和框架模塊所觸發的已棄用警告默認將繼續隱藏。 作為此項更改的結果,標準庫現在允許開發者在三種不同的已棄用警告行為之間進行選擇: - [`FutureWarning`](../library/exceptions.xhtml#FutureWarning "FutureWarning"): 默認情況下總是會顯示,建議用于應用程序最終用戶應該看到的警告信息(例如對于已棄用的應用程序配置的設置選項)。 - [`DeprecationWarning`](../library/exceptions.xhtml#DeprecationWarning "DeprecationWarning"): 默認情況下僅在 [`__main__`](../library/__main__.xhtml#module-__main__ "__main__: The environment where the top-level script is run.") 中以及當運行測試時會顯示,建議用于其他 Python 開發者應該看到的警告信息,提示版本升級可能導致行為改變或者錯誤。 - [`PendingDeprecationWarning`](../library/exceptions.xhtml#PendingDeprecationWarning "PendingDeprecationWarning"): 默認情況下僅在運行測試時會顯示,可用于提示未來版本升級將會改變警告類別為 [`DeprecationWarning`](../library/exceptions.xhtml#DeprecationWarning "DeprecationWarning") 或 [`FutureWarning`](../library/exceptions.xhtml#FutureWarning "FutureWarning") 的情況。 在此之前 [`DeprecationWarning`](../library/exceptions.xhtml#DeprecationWarning "DeprecationWarning") 和 [`PendingDeprecationWarning`](../library/exceptions.xhtml#PendingDeprecationWarning "PendingDeprecationWarning") 都僅在運行測試時可見,這意味著主要編寫單文件腳本或使用 Python 交互模式的開發者可能會因他們所用 API 突然出現的改變而感到驚訝。 參見 [**PEP 565**](https://www.python.org/dev/peps/pep-0565) \[https://www.python.org/dev/peps/pep-0565\] -- 在 `__main__` 中顯示 DeprecationWarningPEP 由 Nick Coghlan 撰寫并實現 ### PEP 560: 對 `typing` 模塊和泛型類型的核心支持 [**PEP 484**](https://www.python.org/dev/peps/pep-0484) \[https://www.python.org/dev/peps/pep-0484\] 最初的設計方式使其不會向核心 CPython 解釋器引入 *任何* 更改。 現在類型提示和 [`typing`](../library/typing.xhtml#module-typing "typing: Support for type hints (see PEP 484).") 模塊已被社區廣泛使用,因此這個限制已被取消。 這個 PEP 引入了兩個特殊方法 [`__class_getitem__()`](../reference/datamodel.xhtml#object.__class_getitem__ "object.__class_getitem__") 和 `__mro_entries__`,這些方法現在被 [`typing`](../library/typing.xhtml#module-typing "typing: Support for type hints (see PEP 484).") 中的大多數類和特殊構造所使用。 結果就是與類型相關的各類操作的速度提升了 7 倍,泛型類型可以在沒有元類沖突的情況下被使用,而 [`typing`](../library/typing.xhtml#module-typing "typing: Support for type hints (see PEP 484).") 模塊中幾個長期存在的錯誤也已被修正。 參見 [**PEP 560**](https://www.python.org/dev/peps/pep-0560) \[https://www.python.org/dev/peps/pep-0560\] -- 對 typing 模塊和泛型類型的核心支持PEP 由 Ivan Levkivskyi 撰寫并實現 ### PEP 552: 基于哈希值的 .pyc 文件 傳統上 Python 檢查字節碼緩存文件 (即 `.pyc` 文件) 是否最新的方式是通過對源碼元數據 (最后更改的時間戳和大小)和生成緩存時保存在其文件頭中的源碼元數據進行比較。 這種檢查方法雖然有效,但也存在缺點。 當文件系統的時間戳太粗糙時,Python 有可能錯過源碼更新,導致用戶感到困惑。 此外,在緩存文件中存在時間戳對于 [構建可再現](https://reproducible-builds.org/) \[https://reproducible-builds.org/\] 并且基于內容的構建系統來說是有問題的。 [**PEP 552**](https://www.python.org/dev/peps/pep-0552) \[https://www.python.org/dev/peps/pep-0552\] 擴展了 pyc 格式以允許使用源文件的哈希值而非源文件的時間戳來檢查有效性。 這種 `.pyc` 文件就稱為“基于哈希值的”。 默認情況下,Python 仍然使用基于時間戳的有效性檢查,不會在運行時生成基于哈希值的 `.pyc` 文件。 基于哈希值的 `.pyc` 文件可以使用 [`py_compile`](../library/py_compile.xhtml#module-py_compile "py_compile: Generate byte-code files from Python source files.") 或 [`compileall`](../library/compileall.xhtml#module-compileall "compileall: Tools for byte-compiling all Python source files in a directory tree.") 來生成。 基于哈希值的 `.pyc` 文件包含兩種變體:已選定和未選定。 Python 會在運行時針對相應源碼文件驗證已選定基于哈希值的 `.pyc` 文件,但對未選定基于哈希值的 pyc 文件則不會這樣做。 未選定基于哈希值的 `.pyc` 文件對于需要由 Python 外部的系統(例如構建系統)負責使 `.pyc` 文件保持最新的環境來說是一種有用的性能優化。 請參閱 [已緩存字節碼的失效](../reference/import.xhtml#pyc-invalidation) 了解更多信息。 參見 [**PEP 552**](https://www.python.org/dev/peps/pep-0552) \[https://www.python.org/dev/peps/pep-0552\] -- 確定性的 pyc 文件PEP 由 Benjamin Peterson 撰寫并實現 ### PEP 545: Python 文檔翻譯 [**PEP 545**](https://www.python.org/dev/peps/pep-0545) \[https://www.python.org/dev/peps/pep-0545\] 描述了創建和維護 Python 文檔翻譯的整個過程。 新增了三個新的翻譯版本: - 日語: <https://docs.python.org/ja/> - 法語: <https://docs.python.org/fr/> - 韓語: <https://docs.python.org/ko/> 參見 [**PEP 545**](https://www.python.org/dev/peps/pep-0545) \[https://www.python.org/dev/peps/pep-0545\] -- Python 文檔翻譯PEP 由 Julien Palard, Inada Naoki 和 Victor Stinner 撰寫并實現。 ### 開發運行時模式: -X dev 新的 [`-X`](../using/cmdline.xhtml#id5)`dev` 命令行選項或新的 [`PYTHONDEVMODE`](../using/cmdline.xhtml#envvar-PYTHONDEVMODE) 環境變量可被用來啟用 CPython 的 *開發模式*。 在開發模式下,CPython 會執行額外的在默認情況下開銷過大的運行時檢查。 請參閱 [`-X`](../using/cmdline.xhtml#id5)`dev` 文檔查看對于此模式效果的完整描述。 ## 其他語言特性修改 - 現在可以將超過 255 個參數傳遞給一個函數,而現在一個函數也可以擁有超過 255 個形參。 (由 Serhiy Storchaka 在 [bpo-12844](https://bugs.python.org/issue12844) \[https://bugs.python.org/issue12844\] 和 [bpo-18896](https://bugs.python.org/issue18896) \[https://bugs.python.org/issue18896\] 中貢獻。) - 現在 [`bytes.fromhex()`](../library/stdtypes.xhtml#bytes.fromhex "bytes.fromhex") 和 [`bytearray.fromhex()`](../library/stdtypes.xhtml#bytearray.fromhex "bytearray.fromhex") 會忽略所有 ASCII 空白符,而非僅是空格符. (由 Robert Xiao 在 [bpo-28927](https://bugs.python.org/issue28927) \[https://bugs.python.org/issue28927\] 中貢獻。) - [`str`](../library/stdtypes.xhtml#str "str"), [`bytes`](../library/stdtypes.xhtml#bytes "bytes") 和 [`bytearray`](../library/stdtypes.xhtml#bytearray "bytearray") 獲得了對新 [`isascii()`](../library/stdtypes.xhtml#str.isascii "str.isascii") 方法的支持,該方法可被用來測試是個字符串或字節串是否僅包含 ASCII 字符。 (由 INADA Naoki 在 [bpo-32677](https://bugs.python.org/issue32677) \[https://bugs.python.org/issue32677\] 中貢獻。) - 現在當 `from ... import ...` 失敗時 [`ImportError`](../library/exceptions.xhtml#ImportError "ImportError") 會顯示模塊名稱和模塊 `__file__` 路徑。 (由 Matthias Bussonnier 在 [bpo-29546](https://bugs.python.org/issue29546) \[https://bugs.python.org/issue29546\] 中貢獻。) - 現在已支持涉及將子模塊綁定到一個名稱的絕對導入的循環導入。 (由 Serhiy Storchaka 在 [bpo-30024](https://bugs.python.org/issue30024) \[https://bugs.python.org/issue30024\] 中貢獻。) - 現在 `object.__format__(x, '')` 等價于 `str(x)` 而非 `format(str(self), '')`。 (由 Serhiy Storchaka d [bpo-28974](https://bugs.python.org/issue28974) \[https://bugs.python.org/issue28974\] 中貢獻。) - 為更好地支持棧跟蹤的動態創建,現在 [`types.TracebackType`](../library/types.xhtml#types.TracebackType "types.TracebackType") 可以從 Python 代碼中被實例化,并且 [回溯對象](../reference/datamodel.xhtml#traceback-objects) 的 `tb_next` 屬性現在是可寫的。 (由 Nathaniel J. Smith 在 [bpo-30579](https://bugs.python.org/issue30579) \[https://bugs.python.org/issue30579\] 中貢獻。) - 當使用 [`-m`](../using/cmdline.xhtml#cmdoption-m) 開關時,現在 `sys.path[0]` 會主動擴展為完整的起始目錄路徑,而不是保持為空目錄(這將允許在發生導入時從 *當前* 工作目錄導入) (由 Nick Coghlan 在 [bpo-33053](https://bugs.python.org/issue33053) \[https://bugs.python.org/issue33053\] 中貢獻。) - 新的 [`-X`](../using/cmdline.xhtml#id5)`importtime` 選項或 [`PYTHONPROFILEIMPORTTIME`](../using/cmdline.xhtml#envvar-PYTHONPROFILEIMPORTTIME) 環境變量可被用來顯示每次模塊導入的時間。 (由 Victor Stinner 在 [bpo-31415](https://bugs.python.org/issue31415) \[https://bugs.python.org/issue31415\] 中貢獻。) ## 新增模塊 ### contextvars 新的 [`contextvars`](../library/contextvars.xhtml#module-contextvars "contextvars: Context Variables") 模塊和一組 [新的 C API](../c-api/contextvars.xhtml#contextvarsobjects) 引入了對 *上下文變量* 的支持。 上下文變量在概念上類似于線程局部變量。 與 TLS 不同,上下文變量能正確地支持異步代碼。 [`asyncio`](../library/asyncio.xhtml#module-asyncio "asyncio: Asynchronous I/O.") 和 [`decimal`](../library/decimal.xhtml#module-decimal "decimal: Implementation of the General Decimal Arithmetic Specification.") 已得到更新以使用和支持開箱即用的上下文變量。 特別是激活的 decimal 上下文現在將存儲在上下文變量中,它允許十進制運算在異步代碼中使用正確的上下文。 參見 [**PEP 567**](https://www.python.org/dev/peps/pep-0567) \[https://www.python.org/dev/peps/pep-0567\] -- 上下文變量PEP 由 Yury Selivanov 撰寫并實現 ### dataclasses 新的 [`dataclass()`](../library/dataclasses.xhtml#dataclasses.dataclass "dataclasses.dataclass") 裝飾器提供了一種聲明 *數據類* 的方式。 數據類使用變量標注來描述其屬性。 它的構造器和其他魔術方法例如 [`__repr__()`](../reference/datamodel.xhtml#object.__repr__ "object.__repr__"), [`__eq__()`](../reference/datamodel.xhtml#object.__eq__ "object.__eq__") 以及 [`__hash__()`](../reference/datamodel.xhtml#object.__hash__ "object.__hash__") 會自動地生成。 示例: ``` @dataclass class Point: x: float y: float z: float = 0.0 p = Point(1.5, 2.5) print(p) # produces "Point(x=1.5, y=2.5, z=0.0)" ``` 參見 [**PEP 557**](https://www.python.org/dev/peps/pep-0557) \[https://www.python.org/dev/peps/pep-0557\] -- 數據類PEP 由 Eric V. Smith 撰寫并實現 ### importlib.resources 新的 [`importlib.resources`](../library/importlib.xhtml#module-importlib.resources "importlib.resources: Package resource reading, opening, and access") 模塊提供了一些新的 API 和一個新的 ABC 用于訪問、打開和讀取包內的 *資源*。 資源基本上類似于包內的文件,但它們不一定是物理文件系統中實際的文件。 模塊加載器可以提供 `get_resource_reader()` 函數,它會返回一個 [`importlib.abc.ResourceReader`](../library/importlib.xhtml#importlib.abc.ResourceReader "importlib.abc.ResourceReader") 實例來支持這個新 API。 內置的文件路徑加載器和 zip 文件加載器都支持此特性。 由 Barry Warsaw 和 Brett Cannon 在 [bpo-32248](https://bugs.python.org/issue32248) \[https://bugs.python.org/issue32248\] 中貢獻。 參見 [importlib\_resources](http://importlib-resources.readthedocs.io/en/latest/) \[http://importlib-resources.readthedocs.io/en/latest/\] -- 用于先前 Python 版本的 PyPI 下層接口。 ## 改進的模塊 ### argparse 新的 [`ArgumentParser.parse_intermixed_args()`](../library/argparse.xhtml#argparse.ArgumentParser.parse_intermixed_args "argparse.ArgumentParser.parse_intermixed_args") 方法允許混合選項與位置參數。 (由 paul.j3 在 [bpo-14191](https://bugs.python.org/issue14191) \[https://bugs.python.org/issue14191\] 中提供。) ### asyncio [`asyncio`](../library/asyncio.xhtml#module-asyncio "asyncio: Asynchronous I/O.") 模塊獲得了許多新的特性、可用性和 [性能提升](#whatsnew37-asyncio-perf)。 重要的改變包括: - 新的 [暫定](../glossary.xhtml#term-provisional-api) [`asyncio.run()`](../library/asyncio-task.xhtml#asyncio.run "asyncio.run") 函數可被用于通過自動創建和銷毀事件循環以基于同步代碼運行協程。 (由 Yury Selivanov 在 [bpo-32314](https://bugs.python.org/issue32314) \[https://bugs.python.org/issue32314\] 中貢獻。) - asyncio 增加支持 [`contextvars`](../library/contextvars.xhtml#module-contextvars "contextvars: Context Variables"). [`loop.call_soon()`](../library/asyncio-eventloop.xhtml#asyncio.loop.call_soon "asyncio.loop.call_soon"), [`loop.call_soon_threadsafe()`](../library/asyncio-eventloop.xhtml#asyncio.loop.call_soon_threadsafe "asyncio.loop.call_soon_threadsafe"), [`loop.call_later()`](../library/asyncio-eventloop.xhtml#asyncio.loop.call_later "asyncio.loop.call_later"), [`loop.call_at()`](../library/asyncio-eventloop.xhtml#asyncio.loop.call_at "asyncio.loop.call_at") 并且 [`Future.add_done_callback()`](../library/asyncio-future.xhtml#asyncio.Future.add_done_callback "asyncio.Future.add_done_callback") 具有新的可選僅關鍵字參數 *context*。 現在 [`Tasks`](../library/asyncio-task.xhtml#asyncio.Task "asyncio.Task") 會自動跟蹤其上下文。 詳情參見 [**PEP 567**](https://www.python.org/dev/peps/pep-0567) \[https://www.python.org/dev/peps/pep-0567\]。 (由 Yury Selivanov 在 [bpo-32436](https://bugs.python.org/issue32436) \[https://bugs.python.org/issue32436\] 中貢獻。) - 增加了新的 [`asyncio.create_task()`](../library/asyncio-task.xhtml#asyncio.create_task "asyncio.create_task") 函數作為 `asyncio.get_event_loop().create_task()` 的快捷方式。 (由 Andrew Svetlov 在 [bpo-32311](https://bugs.python.org/issue32311) \[https://bugs.python.org/issue32311\] 中貢獻。) - 新的 [`loop.start_tls()`](../library/asyncio-eventloop.xhtml#asyncio.loop.start_tls "asyncio.loop.start_tls") 方法可用于升級現有的 TLS 連接。 (由 Yury Selivanov 在 [bpo-23749](https://bugs.python.org/issue23749) \[https://bugs.python.org/issue23749\] 中貢獻。) - 新的 [`loop.sock_recv_into()`](../library/asyncio-eventloop.xhtml#asyncio.loop.sock_recv_into "asyncio.loop.sock_recv_into") 方法允許直接從套接字讀取數據放入所提供的緩沖區,從而可以減少數據復制。 (由 Antoine Pitrou 在 [bpo-31819](https://bugs.python.org/issue31819) \[https://bugs.python.org/issue31819\] 中貢獻。) - 新的 [`asyncio.current_task()`](../library/asyncio-task.xhtml#asyncio.current_task "asyncio.current_task") 函數可返回當前運行的 [`Task`](../library/asyncio-task.xhtml#asyncio.Task "asyncio.Task") 實例,以及新的 [`asyncio.all_tasks()`](../library/asyncio-task.xhtml#asyncio.all_tasks "asyncio.all_tasks") 函數可返回給定循環中所有現存 `Task` 實例的集合。 [`Task.current_task()`](../library/asyncio-task.xhtml#asyncio.Task.current_task "asyncio.Task.current_task") 和 [`Task.all_tasks()`](../library/asyncio-task.xhtml#asyncio.Task.all_tasks "asyncio.Task.all_tasks") 方法已棄用。 (由 Andrew Svetlov 在 [bpo-32250](https://bugs.python.org/issue32250) \[https://bugs.python.org/issue32250\] 中貢獻。) - 新的 *暫定* [`BufferedProtocol`](../library/asyncio-protocol.xhtml#asyncio.BufferedProtocol "asyncio.BufferedProtocol") 類允許通過手動控制接收緩沖區來實現流式協議。 (由 Yury Selivanov 在 [bpo-32251](https://bugs.python.org/issue32251) \[https://bugs.python.org/issue32251\] 中貢獻。) - 新的 [`asyncio.get_running_loop()`](../library/asyncio-eventloop.xhtml#asyncio.get_running_loop "asyncio.get_running_loop") 函數可返回當前運行的循環,如果沒有循環在運行則引發 [`RuntimeError`](../library/exceptions.xhtml#RuntimeError "RuntimeError")。 這與 [`asyncio.get_event_loop()`](../library/asyncio-eventloop.xhtml#asyncio.get_event_loop "asyncio.get_event_loop") 不同,后者在沒有循環在運行時將 *創建* 一個新的事件循環。 (由 Yury Selivanov 在 [bpo-32269](https://bugs.python.org/issue32269) \[https://bugs.python.org/issue32269\] 中提供。) - 新的 [`StreamWriter.wait_closed()`](../library/asyncio-stream.xhtml#asyncio.StreamWriter.wait_closed "asyncio.StreamWriter.wait_closed") 協程方法允許執行等待直到流寫入器被關閉。 新的 [`StreamWriter.is_closing()`](../library/asyncio-stream.xhtml#asyncio.StreamWriter.is_closing "asyncio.StreamWriter.is_closing") 方法可用于確定寫入器是否被關閉。 (由 Andrew Svetlov 在 [bpo-32391](https://bugs.python.org/issue32391) \[https://bugs.python.org/issue32391\] 中貢獻。) - 新的 [`loop.sock_sendfile()`](../library/asyncio-eventloop.xhtml#asyncio.loop.sock_sendfile "asyncio.loop.sock_sendfile") 協程方法允許在可能的情況下使用 [`os.sendfile`](../library/os.xhtml#os.sendfile "os.sendfile") 發送文件。 (由 Andrew Svetlov 在 [bpo-32410](https://bugs.python.org/issue32410) \[https://bugs.python.org/issue32410\] 中貢獻。) - 新的 [`Future.get_loop()`](../library/asyncio-future.xhtml#asyncio.Future.get_loop "asyncio.Future.get_loop") 和 `Task.get_loop()` 方法會返回創建 task 或 future 對象的事件循環的實例。 [`Server.get_loop()`](../library/asyncio-eventloop.xhtml#asyncio.Server.get_loop "asyncio.Server.get_loop") 允許為 [`asyncio.Server`](../library/asyncio-eventloop.xhtml#asyncio.Server "asyncio.Server") 對象執行同樣操作。 (由 Yury Selivanov 在 [bpo-32415](https://bugs.python.org/issue32415) \[https://bugs.python.org/issue32415\] 中,以及由 Srinivas Reddy Thatiparthy 在 [bpo-32418](https://bugs.python.org/issue32418) \[https://bugs.python.org/issue32418\] 中貢獻。) - 現在可以控制 [`asyncio.Server`](../library/asyncio-eventloop.xhtml#asyncio.Server "asyncio.Server") 的實例如何開啟服務。 之前,服務在創建后將立即開啟服務。 新的 *start\_serving* 關鍵字參數已添加到 [`loop.create_server()`](../library/asyncio-eventloop.xhtml#asyncio.loop.create_server "asyncio.loop.create_server") 和 [`loop.create_unix_server()`](../library/asyncio-eventloop.xhtml#asyncio.loop.create_unix_server "asyncio.loop.create_unix_server"),并且 [`Server.start_serving()`](../library/asyncio-eventloop.xhtml#asyncio.Server.start_serving "asyncio.Server.start_serving"), 和 [`Server.serve_forever()`](../library/asyncio-eventloop.xhtml#asyncio.Server.serve_forever "asyncio.Server.serve_forever") 可被用來分離服務的實例化和服務的開啟。 新的 [`Server.is_serving()`](../library/asyncio-eventloop.xhtml#asyncio.Server.is_serving "asyncio.Server.is_serving") 方法會在服務開啟時返回 `True`。 現在 [`Server`](../library/asyncio-eventloop.xhtml#asyncio.Server "asyncio.Server") 對象已是異步上下文管理器: ``` srv = await loop.create_server(...) async with srv: # some code # At this point, srv is closed and no longer accepts new connections. ``` (由 Yury Selivanov 在 [bpo-32662](https://bugs.python.org/issue32662) \[https://bugs.python.org/issue32662\] 中貢獻。) - 由 [`loop.call_later()`](../library/asyncio-eventloop.xhtml#asyncio.loop.call_later "asyncio.loop.call_later") 所返回的回調對象已獲得新的 [`when()`](../library/asyncio-eventloop.xhtml#asyncio.TimerHandle.when "asyncio.TimerHandle.when") 方法,該方法會返回一個排入計劃日程的絕對時間戳。 (由 Andrew Svetlov 在 [bpo-32741](https://bugs.python.org/issue32741) \[https://bugs.python.org/issue32741\] 中貢獻。) - [`loop.create_datagram_endpoint() `](../library/asyncio-eventloop.xhtml#asyncio.loop.create_datagram_endpoint "asyncio.loop.create_datagram_endpoint") 方法已獲得對 Unix 套接字的支持。 (由 Quentin Dawans 在 [bpo-31245](https://bugs.python.org/issue31245) \[https://bugs.python.org/issue31245\] 中貢獻。) - [`asyncio.open_connection()`](../library/asyncio-stream.xhtml#asyncio.open_connection "asyncio.open_connection"), [`asyncio.start_server()`](../library/asyncio-stream.xhtml#asyncio.start_server "asyncio.start_server") functions, [`loop.create_connection()`](../library/asyncio-eventloop.xhtml#asyncio.loop.create_connection "asyncio.loop.create_connection"), [`loop.create_server()`](../library/asyncio-eventloop.xhtml#asyncio.loop.create_server "asyncio.loop.create_server"), [`loop.create_accepted_socket()`](../library/asyncio-eventloop.xhtml#asyncio.loop.connect_accepted_socket "asyncio.loop.connect_accepted_socket") 方法及其對應的 UNIX 套接字變體現在接受 *ssl\_handshake\_timeout* 關鍵字參數。 (由 Neil Aspinall 在 [bpo-29970](https://bugs.python.org/issue29970) \[https://bugs.python.org/issue29970\] 中貢獻。) - 新的 [`Handle.cancelled()`](../library/asyncio-eventloop.xhtml#asyncio.Handle.cancelled "asyncio.Handle.cancelled") 方法會在回調被取消時返回 `True`。 (由 Marat Sharafutdinov 在 [bpo-31943](https://bugs.python.org/issue31943) \[https://bugs.python.org/issue31943\] 中貢獻。) - asyncio 源已被轉換為使用 [`async`](../reference/compound_stmts.xhtml#async)/[`await`](../reference/expressions.xhtml#await) 語法。 (由 Andrew Svetlov 在 [bpo-32193](https://bugs.python.org/issue32193) \[https://bugs.python.org/issue32193\] 中貢獻。) - 新的 [`ReadTransport.is_reading()`](../library/asyncio-protocol.xhtml#asyncio.ReadTransport.is_reading "asyncio.ReadTransport.is_reading") 方法可用于確定傳輸的讀取狀態。 此外,對 [`ReadTransport.resume_reading()`](../library/asyncio-protocol.xhtml#asyncio.ReadTransport.resume_reading "asyncio.ReadTransport.resume_reading") 和 [`ReadTransport.pause_reading()`](../library/asyncio-protocol.xhtml#asyncio.ReadTransport.pause_reading "asyncio.ReadTransport.pause_reading") 的調用現在是冪等的。 (由 Yury Selivanov 在 [bpo-32356](https://bugs.python.org/issue32356) \[https://bugs.python.org/issue32356\] 中貢獻。) - 接受套接字路徑的循環方法現在支持傳入 [路徑類對象](../glossary.xhtml#term-path-like-object)。 (由 Yury Selivanov 在 [bpo-32066](https://bugs.python.org/issue32066) \[https://bugs.python.org/issue32066\] 中貢獻。) - 在 [`asyncio`](../library/asyncio.xhtml#module-asyncio "asyncio: Asynchronous I/O.") 中,Linux 上的 TCP 套接字現在創建時默認帶有 `TCP_NODELAY` 旗標設置。 (由 Yury Selivanov 和 Victor Stinner 在 [bpo-27456](https://bugs.python.org/issue27456) \[https://bugs.python.org/issue27456\] 中貢獻。) - 在被取消任務中發生的異常不會再被記錄。 (由 Yury Selivanov 在 [bpo-30508](https://bugs.python.org/issue30508) \[https://bugs.python.org/issue30508\] 中貢獻。) - 新的 `WindowsSelectorEventLoopPolicy` 和 `WindowsProactorEventLoopPolicy` 類。 (由 Yury Selivanov 在 [bpo-33792](https://bugs.python.org/issue33792) \[https://bugs.python.org/issue33792\] 中貢獻。) 部分 `asyncio` API 改為 [已棄用](#whatsnew37-asyncio-deprecated)。 ### binascii [`b2a_uu()`](../library/binascii.xhtml#binascii.b2a_uu "binascii.b2a_uu") 函數現在接受可選的 *backtick* 關鍵字參數。 當其為真值時,零會以 `'`'` 而非空格來表示。 (由 Xiang Zhang 在 [bpo-30103](https://bugs.python.org/issue30103) \[https://bugs.python.org/issue30103\] 中貢獻。) ### calendar [`HTMLCalendar`](../library/calendar.xhtml#calendar.HTMLCalendar "calendar.HTMLCalendar") 類具有新的類屬性,可以簡化所生成 HTML 日歷中 CSS 類的自定義。 (由 Oz Tiram 在 [bpo-30095](https://bugs.python.org/issue30095) \[https://bugs.python.org/issue30095\] 中貢獻。) ### collections `collections.namedtuple()` 現在支持默認值。 (由 Raymond Hettinger 在 [bpo-32320](https://bugs.python.org/issue32320) \[https://bugs.python.org/issue32320\] 中貢獻。) ### compileall [`compileall.compile_dir()`](../library/compileall.xhtml#compileall.compile_dir "compileall.compile_dir") 增加了新的 *invalidation\_mode* 形參,可用于啟用 [基于哈希值的 .pyc 有效性檢測](#whatsnew37-pep552)。 失效模式也可以在命令行中使用新的 `--invalidation-mode` 參數來指定。 (由 Benjamin Peterson 在 [bpo-31650](https://bugs.python.org/issue31650) \[https://bugs.python.org/issue31650\] 中貢獻。) ### concurrent.futures [`ProcessPoolExecutor`](../library/concurrent.futures.xhtml#concurrent.futures.ProcessPoolExecutor "concurrent.futures.ProcessPoolExecutor") 和 [`ThreadPoolExecutor`](../library/concurrent.futures.xhtml#concurrent.futures.ThreadPoolExecutor "concurrent.futures.ThreadPoolExecutor") 現在支持新的 *初始化器* 以及 *initargs* 構造器參數。 (由 Antoine Pitrou 在 [bpo-21423](https://bugs.python.org/issue21423) \[https://bugs.python.org/issue21423\] 中貢獻。) [`ProcessPoolExecutor`](../library/concurrent.futures.xhtml#concurrent.futures.ProcessPoolExecutor "concurrent.futures.ProcessPoolExecutor") 現在能通過新的 *mp\_context* 參數來接受多進程上下文。 (由 Thomas Moreau 在 [bpo-31540](https://bugs.python.org/issue31540) \[https://bugs.python.org/issue31540\] 中貢獻。) ### contextlib 新的 [`nullcontext()`](../library/contextlib.xhtml#contextlib.nullcontext "contextlib.nullcontext") 是一個比 [`ExitStack`](../library/contextlib.xhtml#contextlib.ExitStack "contextlib.ExitStack") 更簡單和快速的無操作上下文管理器。 (由 Jesse-Bakker 在 [bpo-10049](https://bugs.python.org/issue10049) \[https://bugs.python.org/issue10049\] 中貢獻。) 增加了新的 [`asynccontextmanager()`](../library/contextlib.xhtml#contextlib.asynccontextmanager "contextlib.asynccontextmanager"), [`AbstractAsyncContextManager`](../library/contextlib.xhtml#contextlib.AbstractAsyncContextManager "contextlib.AbstractAsyncContextManager") 和 [`AsyncExitStack`](../library/contextlib.xhtml#contextlib.AsyncExitStack "contextlib.AsyncExitStack") 以補充它們所對應的同步項。 (由 Jelle Zijlstra 在 [bpo-29679](https://bugs.python.org/issue29679) \[https://bugs.python.org/issue29679\] 和 [bpo-30241](https://bugs.python.org/issue30241) \[https://bugs.python.org/issue30241\] 中,以及由 Alexander Mohr 和 Ilya Kulakov 在 [bpo-29302](https://bugs.python.org/issue29302) \[https://bugs.python.org/issue29302\] 中貢獻。) ### cProfile [`cProfile`](../library/profile.xhtml#module-cProfile "cProfile") 命令行現在接受 `-m module_name` 作為腳本路徑的替代。 (由 Sanyam Khurana 在 [bpo-21862](https://bugs.python.org/issue21862) \[https://bugs.python.org/issue21862\] 中貢獻。) ### crypt [`crypt`](../library/crypt.xhtml#module-crypt "crypt: The crypt() function used to check Unix passwords. (Unix)") 模塊現在支持 Blowfish 哈希方法。 (由 Serhiy Storchaka 在 [bpo-31664](https://bugs.python.org/issue31664) \[https://bugs.python.org/issue31664\] 中貢獻。) [`mksalt()`](../library/crypt.xhtml#crypt.mksalt "crypt.mksalt") 函數現在允許指定哈希操作的輪數。 (由 Serhiy Storchaka 在 [bpo-31702](https://bugs.python.org/issue31702) \[https://bugs.python.org/issue31702\] 中貢獻。) ### datetime 新的 [`datetime.fromisoformat()`](../library/datetime.xhtml#datetime.datetime.fromisoformat "datetime.datetime.fromisoformat") 方法會基于由 `datetime.isoformat()` 所輸出的某一特定格式字符串構建 [`datetime`](../library/datetime.xhtml#datetime.datetime "datetime.datetime") 對象。 (由 Paul Ganssle 在 [bpo-15873](https://bugs.python.org/issue15873) \[https://bugs.python.org/issue15873\] 中貢獻。) [`tzinfo`](../library/datetime.xhtml#datetime.tzinfo "datetime.tzinfo") 類現在支持小于一分鐘的偏移量。 (由 Alexander Belopolsky 在 [bpo-5288](https://bugs.python.org/issue5288) \[https://bugs.python.org/issue5288\] 中貢獻。) ### dbm [`dbm.dumb`](../library/dbm.xhtml#module-dbm.dumb "dbm.dumb: Portable implementation of the simple DBM interface.") 現在支持讀取只讀文件,并且在其未改變時不再寫入索引文件。 ### decimal [`decimal`](../library/decimal.xhtml#module-decimal "decimal: Implementation of the General Decimal Arithmetic Specification.") 模塊現在使用 [上下文變量](#whatsnew37-pep567) 來儲存十進制值上下文。 (由 Yury Selivanov 在 [bpo-32630](https://bugs.python.org/issue32630) \[https://bugs.python.org/issue32630\] 中貢獻。) ### dis [`dis()`](../library/dis.xhtml#dis.dis "dis.dis") 函數現在能夠反匯編嵌套的代碼對象(推導式、生成器表達式和嵌套函數的代碼,以及用于構建嵌套類的代碼)。 反匯編遞歸的最大深度由新的 *depth* 形參來控制。 (由 Serhiy Storchaka 在 [bpo-11822](https://bugs.python.org/issue11822) \[https://bugs.python.org/issue11822\] 中貢獻。) ### distutils `README.rst` 現在包含在 distutils 的標準 README 列表之中,因而也包含在源碼發布之中。 (由 Ryan Gonzalez 在 [bpo-11913](https://bugs.python.org/issue11913) \[https://bugs.python.org/issue11913\] 中貢獻。) ### enum [`Enum`](../library/enum.xhtml#enum.Enum "enum.Enum") 增加了新的 `_ignore_` 類特征屬性,該屬性允許列出不應當成為枚舉成員的特征屬性名稱。 (由 Ethan Furman 在 [bpo-31801](https://bugs.python.org/issue31801) \[https://bugs.python.org/issue31801\] 中貢獻。) 在 Python 3.8 中,嘗試在 `Enum` 類中檢查非 Enum 對象將引發 [`TypeError`](../library/exceptions.xhtml#TypeError "TypeError") (例如 `1 in Color`);類似地,嘗試在 `Flag` 成員中檢查非 Flag 對象也將引發 [`TypeError`](../library/exceptions.xhtml#TypeError "TypeError") (例如 `1 in Perm.RW`);目前,兩種操作均會返回 [`False`](../library/constants.xhtml#False "False") 并且已棄用。 (由 Ethan Furman 在 [bpo-33217](https://bugs.python.org/issue33217) \[https://bugs.python.org/issue33217\] 中貢獻。) ### functools [`functools.singledispatch()`](../library/functools.xhtml#functools.singledispatch "functools.singledispatch") 現在支持使用類型標注來注冊實現。 (由 ?ukasz Langa 在 [bpo-32227](https://bugs.python.org/issue32227) \[https://bugs.python.org/issue32227\] 中貢獻。) ### gc 新的 [`gc.freeze()`](../library/gc.xhtml#gc.freeze "gc.freeze") 函數允許凍結由垃圾回收器所跟蹤的所有對象,并將它們從未來的集合中排除。 這可以在 POSIX `fork()` 調用之前使用以令 GC 友好地進行寫入時復制或加速收集。 新的 [`gc.unfreeze()`](../library/gc.xhtml#gc.unfreeze "gc.unfreeze") 函數會反轉此操作。 此外,[`gc.get_freeze_count()`](../library/gc.xhtml#gc.get_freeze_count "gc.get_freeze_count") 可被用于獲取凍結對象的數量。 (由 Li Zekun 在 [bpo-31558](https://bugs.python.org/issue31558) \[https://bugs.python.org/issue31558\] 中貢獻。) ### hmac [`hmac`](../library/hmac.xhtml#module-hmac "hmac: Keyed-Hashing for Message Authentication (HMAC) implementation") 現在具有經優化的一次性 [`digest()`](../library/hmac.xhtml#hmac.digest "hmac.digest") 函數,其速度比 `HMAC()` 要快三倍。 (由 Christian Heimes 在 [bpo-32433](https://bugs.python.org/issue32433) \[https://bugs.python.org/issue32433\] 中貢獻。) ### http.client [`HTTPConnection`](../library/http.client.xhtml#http.client.HTTPConnection "http.client.HTTPConnection") 和 [`HTTPSConnection`](../library/http.client.xhtml#http.client.HTTPSConnection "http.client.HTTPSConnection") 現在支持新的 *blocksize* 參數以提升上傳吞吐量。 (由 Nir Soffer 在 [bpo-31945](https://bugs.python.org/issue31945) \[https://bugs.python.org/issue31945\] 中貢獻。) ### http.server [`SimpleHTTPRequestHandler`](../library/http.server.xhtml#http.server.SimpleHTTPRequestHandler "http.server.SimpleHTTPRequestHandler") 現在支持 HTTP `If-Modified-Since` 標頭。 如果目標文件在該標點指定的時間之后未被修改則服務器會返回 304 響應狀態。 (由 Pierre Quentel 在 [bpo-29654](https://bugs.python.org/issue29654) \[https://bugs.python.org/issue29654\] 中貢獻。) [`SimpleHTTPRequestHandler`](../library/http.server.xhtml#http.server.SimpleHTTPRequestHandler "http.server.SimpleHTTPRequestHandler") 接受新的 *directory* 參數并增加了新的 `--directory` 命令行參數。 通過此形參,服務器可以對服務指定目錄,默認情況下它使用當前工作目錄。 (由 Stéphane Wirtel 和 Julien Palard 在 [bpo-28707](https://bugs.python.org/issue28707) \[https://bugs.python.org/issue28707\] 中貢獻。) 新的 [`ThreadingHTTPServer`](../library/http.server.xhtml#http.server.ThreadingHTTPServer "http.server.ThreadingHTTPServer") 類使用線程來處理使用 `ThreadingMixin` 的請求。 它會在 `http.server` 附帶 `-m` 運行時被使用。 (由 Julien Palard 在 [bpo-31639](https://bugs.python.org/issue31639) \[https://bugs.python.org/issue31639\] 中貢獻。) ### idlelib 與 IDLE 多個對自動補全的修正。 (由 Louie Lu 在 [bpo-15786](https://bugs.python.org/issue15786) \[https://bugs.python.org/issue15786\] 中貢獻。) Module Browser (在 File 菜單中,之前稱為 Class Browser) 現在會在最高層級函數和類之外顯示嵌套的函數和類。 (由 Guilherme Polo, Cheryl Sabella 和 Terry Jan Reedy 在 [bpo-1612262](https://bugs.python.org/issue1612262) \[https://bugs.python.org/issue1612262\] 中貢獻。) Settings 對話框 (Options 中的 Configure IDLE) 已經被部分重寫以改進外觀和功能。 (由 Cheryl Sabella 和 Terry Jan Reedy 在多個問題項中貢獻。) 字體樣本現在包括一組非拉丁字符以便用戶能更好地查看所選特定字體的效果。 (由 Terry Jan Reedy 在 [bpo-13802](https://bugs.python.org/issue13802) \[https://bugs.python.org/issue13802\] 中貢獻。) 樣本可以被修改以包括其他字符。 (由 Serhiy Storchaka 在 [bpo-31860](https://bugs.python.org/issue31860) \[https://bugs.python.org/issue31860\] 中貢獻。) 之前以擴展形式實現的 IDLE 特性已作為正常特性重新實現。 它們的設置已從 Extensions 選項卡移至其他對話框選項卡。 (由 Charles Wohlganger 和 Terry Jan Reedy 在 [bpo-27099](https://bugs.python.org/issue27099) \[https://bugs.python.org/issue27099\] 中實現。) 編輯器代碼上下文選項已經過修改。 Box 會顯示所有上下文行直到最大行數。 點擊一個上下文行會使編輯器跳轉到該行。 自定義主題的上下文顏色已添加到 Settings 對話框的 Highlights 選項卡。 (由 Cheryl Sabella 和 Terry Jan Reedy 在 [bpo-33642](https://bugs.python.org/issue33642) \[https://bugs.python.org/issue33642\], [bpo-33768](https://bugs.python.org/issue33768) \[https://bugs.python.org/issue33768\] 和 [bpo-33679](https://bugs.python.org/issue33679) \[https://bugs.python.org/issue33679\] 中貢獻。) 在 Windows 上,會有新的 API 調用將 tk 對 DPI 的調整告知 Windows。 在 Windows 8.1+ 或 10 上,如果 Python 二進制碼的 DPI 兼容屬性未改變,并且監視器分辨率大于 96 DPI,這應該會令文本和線條更清晰。 否則的話它應該不造成影響。 (由 Terry Jan Reedy 在 [bpo-33656](https://bugs.python.org/issue33656) \[https://bugs.python.org/issue33656\] 中貢獻。) 在 3.7.1 中新增: 超過 N 行(默認值為 50)的輸出將被折疊為一個按鈕。 N 可以在 Settings 對話框的 General 頁的 PyShell 部分中進行修改。 數量較少但是超長的行可以通過在輸出上右擊來折疊。 被折疊的輸出可通過雙擊按鈕來展開,或是通過右擊按鈕來放入剪貼板或是單獨的窗口。 (由 Tal Einat 在 [bpo-1529353](https://bugs.python.org/issue1529353) \[https://bugs.python.org/issue1529353\] 中貢獻。) 上述修改已被反向移植到 3.6 維護發行版中。 ### importlib 引入了 [`importlib.abc.ResourceReader`](../library/importlib.xhtml#importlib.abc.ResourceReader "importlib.abc.ResourceReader") ABC 以支持從包中加載資源。 另請參閱 [importlib.resources](#whatsnew37-importlib-resources)。 (由 Barry Warsaw, Brett Cannon 在 [bpo-32248](https://bugs.python.org/issue32248) \[https://bugs.python.org/issue32248\] 中貢獻。) 如果模塊缺少規格描述 [`importlib.reload()`](../library/importlib.xhtml#importlib.reload "importlib.reload") 現在會引發 [`ModuleNotFoundError`](../library/exceptions.xhtml#ModuleNotFoundError "ModuleNotFoundError")。 (由 Garvit Khatri 在 [bpo-29851](https://bugs.python.org/issue29851) \[https://bugs.python.org/issue29851\] 中貢獻。) 如果指定的父模塊不是一個包 (即缺少 `__path__` 屬性) `importlib.find_spec()` 現在會引發 [`ModuleNotFoundError`](../library/exceptions.xhtml#ModuleNotFoundError "ModuleNotFoundError") 而非 [`AttributeError`](../library/exceptions.xhtml#AttributeError "AttributeError")。 (由 Milan Oberkirch 在 [bpo-30436](https://bugs.python.org/issue30436) \[https://bugs.python.org/issue30436\] 中貢獻。) 新的 `importlib.source_hash()` 可被用來計算傳入源的哈希值。 [基于哈希值的 .pyc 文件](#whatsnew37-pep552) 會嵌入此函數所返回的值。 ### io 新的 [`TextIOWrapper.reconfigure()`](../library/io.xhtml#io.TextIOWrapper.reconfigure "io.TextIOWrapper.reconfigure") 方法可用于根據新的設置重新配置文本流。 (由 Antoine Pitrou 在 [bpo-30526](https://bugs.python.org/issue30526) \[https://bugs.python.org/issue30526\] 以及 INADA Naoki 在 [bpo-15216](https://bugs.python.org/issue15216) \[https://bugs.python.org/issue15216\] 中貢獻。) ### ipaddress methods of [`ipaddress.IPv6Network`](../library/ipaddress.xhtml#ipaddress.IPv6Network "ipaddress.IPv6Network") 和 [`ipaddress.IPv4Network`](../library/ipaddress.xhtml#ipaddress.IPv4Network "ipaddress.IPv4Network") 中新的 `subnet_of()` 以及 `supernet_of()` 方法可用于網絡包含測試。 (由 Michel Albert 和 Cheryl Sabella 在 [bpo-20825](https://bugs.python.org/issue20825) \[https://bugs.python.org/issue20825\] 中貢獻。) ### itertools [`itertools.islice()`](../library/itertools.xhtml#itertools.islice "itertools.islice") 現在接受 [`類整數對象`](../reference/datamodel.xhtml#object.__index__ "object.__index__") 作為 start, stop 和 slice 參數。 (由 Will Roberts 在 [bpo-30537](https://bugs.python.org/issue30537) \[https://bugs.python.org/issue30537\] 中貢獻。) ### locale [`locale.format_string()`](../library/locale.xhtml#locale.format_string "locale.format_string") 中新的 *monetary* 參數可用于轉換所使用的千位分隔符和分組字符串。 (由 Garvit 在 [bpo-10379](https://bugs.python.org/issue10379) \[https://bugs.python.org/issue10379\] 中貢獻。) 現在 [`locale.getpreferredencoding()`](../library/locale.xhtml#locale.getpreferredencoding "locale.getpreferredencoding") 函數在 Android 上或是在 [強制 UTF-8 模式](#whatsnew37-pep540) 下總是返回 `'UTF-8'` 。 ### logging [`Logger`](../library/logging.xhtml#logging.Logger "logging.Logger") 實例現在可以被 pickle。 (由 Vinay Sajip 在 [bpo-30520](https://bugs.python.org/issue30520) \[https://bugs.python.org/issue30520\] 中貢獻。) 新的 [`StreamHandler.setStream()`](../library/logging.handlers.xhtml#logging.StreamHandler.setStream "logging.StreamHandler.setStream") 方法可用于在句柄創建之后替換日志流。 (由 Vinay Sajip 在 [bpo-30522](https://bugs.python.org/issue30522) \[https://bugs.python.org/issue30522\] 中創建。) 現在可以在傳遞給 [`logging.config.fileConfig()`](../library/logging.config.xhtml#logging.config.fileConfig "logging.config.fileConfig") 的配置信息中對句柄構造器指定關鍵字參數。 (由 Preston Landers 在 [bpo-31080](https://bugs.python.org/issue31080) \[https://bugs.python.org/issue31080\] 中貢獻。) ### math 新的 [`math.remainder()`](../library/math.xhtml#math.remainder "math.remainder") 函數實現了 IEEE 754 風格的余數運算。 (由 Mark Dickinson 在 [bpo-29962](https://bugs.python.org/issue29962) \[https://bugs.python.org/issue29962\] 中貢獻。) ### mimetypes .bmp 的 MIME type 從 `'image/x-ms-bmp'` 改為 `'image/bmp'`。 (由 Nitish Chandra 在 [bpo-22589](https://bugs.python.org/issue22589) \[https://bugs.python.org/issue22589\] 中貢獻。) ### msilib 新的 [`Database.Close()`](../library/msilib.xhtml#msilib.Database.Close "msilib.Database.Close") 方法可用于關閉 MSI 數據庫。 (由 Berker Peksag 在 [bpo-20486](https://bugs.python.org/issue20486) \[https://bugs.python.org/issue20486\] 中貢獻。) ### multiprocessing 新的 [`Process.close()`](../library/multiprocessing.xhtml#multiprocessing.Process.close "multiprocessing.Process.close") 方法會顯式地關閉進程對象并釋放與其關聯的所有資源。 如果底層進程仍在運行則將引發 [`ValueError`](../library/exceptions.xhtml#ValueError "ValueError")。 (由 Antoine Pitrou 在 [bpo-30596](https://bugs.python.org/issue30596) \[https://bugs.python.org/issue30596\] 中貢獻。) 新的 [`Process.kill()`](../library/multiprocessing.xhtml#multiprocessing.Process.kill "multiprocessing.Process.kill") 方法可用于在 Unix 上使用 `SIGKILL` 信號來終止進程。 (由 Vitor Pereira 在 [bpo-30794](https://bugs.python.org/issue30794) \[https://bugs.python.org/issue30794\] 中貢獻。) 由 [`Process`](../library/multiprocessing.xhtml#multiprocessing.Process "multiprocessing.Process") 所創建的非守護線程現在會在進程退出時被合并。 (由 Antoine Pitrou 在 [bpo-18966](https://bugs.python.org/issue18966) \[https://bugs.python.org/issue18966\] 中貢獻。) ### os [`os.fwalk()`](../library/os.xhtml#os.fwalk "os.fwalk") 現在接受 [`bytes`](../library/stdtypes.xhtml#bytes "bytes") 類型的 *path* 參數。 (由 Serhiy Storchaka 在 [bpo-28682](https://bugs.python.org/issue28682) \[https://bugs.python.org/issue28682\] 中貢獻。) [`os.scandir()`](../library/os.xhtml#os.scandir "os.scandir") 已獲得對 [文件描述器](../library/os.xhtml#path-fd) 的支持。 (由 Serhiy Storchaka 在 [bpo-25996](https://bugs.python.org/issue25996) \[https://bugs.python.org/issue25996\] 中貢獻。) 新的 [`register_at_fork()`](../library/os.xhtml#os.register_at_fork "os.register_at_fork") 函數允許注冊 Python 回調以便在進程分叉中執行。 (由 Antoine Pitrou 在 [bpo-16500](https://bugs.python.org/issue16500) \[https://bugs.python.org/issue16500\] 中貢獻。) 增加了 [`os.preadv()`](../library/os.xhtml#os.preadv "os.preadv") (結合了 [`os.readv()`](../library/os.xhtml#os.readv "os.readv") 與 [`os.pread()`](../library/os.xhtml#os.pread "os.pread") 的功能) 以及 [`os.pwritev()`](../library/os.xhtml#os.pwritev "os.pwritev") 函數 (結合了 [`os.writev()`](../library/os.xhtml#os.writev "os.writev") 和 [`os.pwrite()`](../library/os.xhtml#os.pwrite "os.pwrite") 的功能)。 (由 Pablo Galindo 在 [bpo-31368](https://bugs.python.org/issue31368) \[https://bugs.python.org/issue31368\] 中貢獻。) [`os.makedirs()`](../library/os.xhtml#os.makedirs "os.makedirs") 的 mode 參數不再影響新創建的中間層級目錄的文件權限。 (由 Serhiy Storchaka 在 [bpo-19930](https://bugs.python.org/issue19930) \[https://bugs.python.org/issue19930\] 中貢獻。) [`os.dup2()`](../library/os.xhtml#os.dup2 "os.dup2") 現在會返回新的文件描述器。 之前,返回的總是 `None`。 (由 Benjamin Peterson 在 [bpo-32441](https://bugs.python.org/issue32441) \[https://bugs.python.org/issue32441\] 中貢獻。) 在 Solaris 及其派生系統上 [`os.stat()`](../library/os.xhtml#os.stat "os.stat") 所返回的結構現在會包含 [`st_fstype`](../library/os.xhtml#os.stat_result.st_fstype "os.stat_result.st_fstype") 屬性。 (由 Jesús Cea Avión 在 [bpo-32659](https://bugs.python.org/issue32659) \[https://bugs.python.org/issue32659\] 中貢獻。) ### pathlib 在 POSIX 類系統上新的 [`Path.is_mount()`](../library/pathlib.xhtml#pathlib.Path.is_mount "pathlib.Path.is_mount") 方法現在可用于確定一個路徑是否為掛載點。 (由 Cooper Ry Lees 在 [bpo-30897](https://bugs.python.org/issue30897) \[https://bugs.python.org/issue30897\] 中貢獻。) ### pdb [`pdb.set_trace()`](../library/pdb.xhtml#pdb.set_trace "pdb.set_trace") 現在接受一個可選的限關鍵字參數 *header*。 如果給出,它會在調試開始之前被打印到控制臺。 (由 Barry Warsaw 在 [bpo-31389](https://bugs.python.org/issue31389) \[https://bugs.python.org/issue31389\] 中貢獻。) [`pdb`](../library/pdb.xhtml#module-pdb "pdb: The Python debugger for interactive interpreters.") 命令行現在接受 `-m module_name` 作為對腳本文件的替代。 (由 Mario Corchero 在 [bpo-32206](https://bugs.python.org/issue32206) \[https://bugs.python.org/issue32206\] 中貢獻。) ### py\_compile [`py_compile.compile()`](../library/py_compile.xhtml#py_compile.compile "py_compile.compile") -- 及其擴展形式 [`compileall`](../library/compileall.xhtml#module-compileall "compileall: Tools for byte-compiling all Python source files in a directory tree.") -- 現在會通過無條件地為基于哈希值的有效性驗證創建 `.pyc` 文件來支持 `SOURCE_DATE_EPOCH` 環境變量。 這樣可以確保當 `.pyc` 文件被主動創建時 [可重現的生成](https://reproducible-builds.org/) \[https://reproducible-builds.org/\]。 (由 Bernhard M. Wiedemann 在 [bpo-29708](https://bugs.python.org/issue29708) \[https://bugs.python.org/issue29708\] 中貢獻。) ### pydoc pydoc 服務器現在可以綁定到由新的 `-n` 命令行參數所指定的任意主機名。 (由 Feanil Patel 在 [bpo-31128](https://bugs.python.org/issue31128) \[https://bugs.python.org/issue31128\] 中貢獻。) ### queue 新的 [`SimpleQueue`](../library/queue.xhtml#queue.SimpleQueue "queue.SimpleQueue") 類是一個無界的 FIFO 隊列。 (由 Antoine Pitrou 在 [bpo-14976](https://bugs.python.org/issue14976) \[https://bugs.python.org/issue14976\] 中貢獻。) ### re 旗標 [`re.ASCII`](../library/re.xhtml#re.ASCII "re.ASCII"), [`re.LOCALE`](../library/re.xhtml#re.LOCALE "re.LOCALE") 和 `re.UNICODE` 可以在組的范圍內設置。 (由 Serhiy Storchaka 在 [bpo-31690](https://bugs.python.org/issue31690) \[https://bugs.python.org/issue31690\] 中貢獻。) [`re.split()`](../library/re.xhtml#re.split "re.split") 現在支持基于匹配一個空字符串的模式例如 `r'\b'`, `'^$'` 或 `(?=-)` 進行拆分。 (由 Serhiy Storchaka 在 [bpo-25054](https://bugs.python.org/issue25054) \[https://bugs.python.org/issue25054\] 中貢獻。) 使用 [`re.LOCALE`](../library/re.xhtml#re.LOCALE "re.LOCALE") 旗標編譯的正則表達式不再依賴于編譯時的區域設置。 區域設置僅在已編譯正則表達式被使用時才被應用。 (由 Serhiy Storchaka 在 [bpo-30215](https://bugs.python.org/issue30215) \[https://bugs.python.org/issue30215\] 中貢獻。) 現在如果一個正則表達式包含語義將在未來發生改變的字符集構造,則會引發 [`FutureWarning`](../library/exceptions.xhtml#FutureWarning "FutureWarning"),例如嵌套集與集合操作等。 (由 Serhiy Storchaka 在 [bpo-30349](https://bugs.python.org/issue30349) \[https://bugs.python.org/issue30349\] 中貢獻。) 已編譯正則表達式和匹配對象現在可以使用 [`copy.copy()`](../library/copy.xhtml#copy.copy "copy.copy") 和 [`copy.deepcopy()`](../library/copy.xhtml#copy.deepcopy "copy.deepcopy") 進行拷貝。 (由 Serhiy Storchaka 在 [bpo-10076](https://bugs.python.org/issue10076) \[https://bugs.python.org/issue10076\] 中貢獻。) ### signal [`signal.set_wakeup_fd()`](../library/signal.xhtml#signal.set_wakeup_fd "signal.set_wakeup_fd") 函數新增的 *warn\_on\_full\_buffer* 參數可以指定當喚醒緩沖區溢出時 Python 是否要在 stderr 上打印警告信息。 (由 Nathaniel J. Smith 在 [bpo-30050](https://bugs.python.org/issue30050) \[https://bugs.python.org/issue30050\] 中貢獻。) ### socket 新增的 [`socket.getblocking()`](../library/socket.xhtml#socket.socket.getblocking "socket.socket.getblocking") 方法會在套接字處于阻塞模式時返回 `True`,否則返回 `False`。 (由 Yury Selivanov 在 [bpo-32373](https://bugs.python.org/issue32373) \[https://bugs.python.org/issue32373\] 中貢獻。) 新的 [`socket.close()`](../library/socket.xhtml#socket.close "socket.close") 函數可關閉所傳入的套接字文件描述符。 應該用此函數來代替 [`os.close()`](../library/os.xhtml#os.close "os.close") 以獲得更好的跨平臺兼容性。 (由 Christian Heimes 在 [bpo-32454](https://bugs.python.org/issue32454) \[https://bugs.python.org/issue32454\] 中貢獻。) [`socket`](../library/socket.xhtml#module-socket "socket: Low-level networking interface.") 模塊現在會公開 `socket.TCP_CONGESTION` (Linux 2.6.13), `socket.TCP_USER_TIMEOUT` (Linux 2.6.37) 以及 `socket.TCP_NOTSENT_LOWAT` (Linux 3.12) 常量。 (由 Omar Sandoval 在 [bpo-26273](https://bugs.python.org/issue26273) \[https://bugs.python.org/issue26273\] 以及 Nathaniel J. Smith 在 [bpo-29728](https://bugs.python.org/issue29728) \[https://bugs.python.org/issue29728\] 中貢獻。) 已加入對 [`socket.AF_VSOCK`](../library/socket.xhtml#socket.AF_VSOCK "socket.AF_VSOCK") 套接字的支持以允許在虛擬機及其宿主機之間進行通訊。 (由 Cathy Avery 在 [bpo-27584](https://bugs.python.org/issue27584) \[https://bugs.python.org/issue27584\] 中貢獻。) 套接字現在默認會根據文件描述符自動檢測所屬族、類型和協議。 (由 Christian Heimes 在 [bpo-28134](https://bugs.python.org/issue28134) \[https://bugs.python.org/issue28134\] 中貢獻。) ### socketserver `socketserver.ThreadingMixIn.server_close()` 現在會等待所有非守護線程完成。 `socketserver.ForkingMixIn.server_close()` 現在會等待所有子進程完成。 為 [`socketserver.ForkingMixIn`](../library/socketserver.xhtml#socketserver.ForkingMixIn "socketserver.ForkingMixIn") 和 [`socketserver.ThreadingMixIn`](../library/socketserver.xhtml#socketserver.ThreadingMixIn "socketserver.ThreadingMixIn") 類增加了新的 `socketserver.ForkingMixIn.block_on_close` 類屬性。 該類屬性值設為 `False` 以保持 3.7 之前的行為。 ### sqlite3 現在當下層的 SQLite 庫版本為 3.6.11 及以上時 [`sqlite3.Connection`](../library/sqlite3.xhtml#sqlite3.Connection "sqlite3.Connection") 會開放 [`backup()`](../library/sqlite3.xhtml#sqlite3.Connection.backup "sqlite3.Connection.backup") 方法。 (由 Lele Gaifax 在 [bpo-27645](https://bugs.python.org/issue27645) \[https://bugs.python.org/issue27645\] 中貢獻。) [`sqlite3.connect()`](../library/sqlite3.xhtml#sqlite3.connect "sqlite3.connect") 的 *database* 參數現在接受任何 [path-like object](../glossary.xhtml#term-path-like-object),而不是只接受字符串。 (由 Anders Lorentsen 在 [bpo-31843](https://bugs.python.org/issue31843) \[https://bugs.python.org/issue31843\] 中貢獻。) ### ssl [`ssl`](../library/ssl.xhtml#module-ssl "ssl: TLS/SSL wrapper for socket objects") 模塊現在使用 OpenSSL 的內置 API 代替 [`match_hostname()`](../library/ssl.xhtml#ssl.match_hostname "ssl.match_hostname") 來檢查主機名或 IP 地址。 值的驗證會在 TLS 握手期間進行。 任何證書驗證錯誤包括主機名檢查失敗現在將引發 [`SSLCertVerificationError`](../library/ssl.xhtml#ssl.SSLCertVerificationError "ssl.SSLCertVerificationError") 并使用正確的 TLS Alert 消息中止握手過程。 這個新異常包含有額外的信息。 主機名驗證可通過 `SSLContext.host_flags` 進行自定義。 (由 Christian Heimes 在 [bpo-31399](https://bugs.python.org/issue31399) \[https://bugs.python.org/issue31399\] 中貢獻。) 注解 改進的主機名檢測需要有兼容 OpenSSL 1.0.2 或 1.1 的 *libssl* 實現。 因此,OpenSSL 0.9.8 和 1.0.1 不再被支持(請參閱 [平臺支持的移除](#platform-support-removals) 了解詳情)。 目前 ssl 模塊主要兼容 LibreSSL 2.7.2 及更高版本。 `ssl` 模塊不再以 SNI TLS 擴展發送 IP 地址。 (由 Christian Heimes 在 [bpo-32185](https://bugs.python.org/issue32185) \[https://bugs.python.org/issue32185\] 中貢獻。) [`match_hostname()`](../library/ssl.xhtml#ssl.match_hostname "ssl.match_hostname") 不再支持部分通配符例如 `www*.example.org`。 `SSLContext.host_flags` 默認已禁用部分通配符匹配。 (由 Mandeep Singh 在 [bpo-23033](https://bugs.python.org/issue23033) \[https://bugs.python.org/issue23033\] 以及 Christian Heimes 在 [bpo-31399](https://bugs.python.org/issue31399) \[https://bugs.python.org/issue31399\] 中貢獻。) `ssl` 模塊默認的加密套件選擇現在是使用黑名單方式而非硬編碼的白名單。 Python 不會再重新啟用已經被 OpenSSL 安全更新所阻止的加密。 默認的加密套件選擇可以在編譯時進行配置。 (由 Christian Heimes 在 [bpo-31429](https://bugs.python.org/issue31429) \[https://bugs.python.org/issue31429\] 中貢獻。) 現在已支持包含國際化域名 (IDN) 的服務器證書驗證。 作為此更改的一部分,[`SSLSocket.server_hostname`](../library/ssl.xhtml#ssl.SSLSocket.server_hostname "ssl.SSLSocket.server_hostname") 屬性現在會以預期的 A 標簽形式 (`"xn--pythn-mua.org"`) 而不是以 U 標簽形式 (`"pyth?n.org"`) 存儲。 (由 Nathaniel J. Smith 與 Christian Heimes 在 [bpo-28414](https://bugs.python.org/issue28414) \[https://bugs.python.org/issue28414\] 中貢獻。) `ssl` 模塊對 TLS 1.3 和 OpenSSL 1.1.1 具有初步和實驗性的支持。 在 Python 3.7.0 發布的時刻,OpenSSL 1.1.1 仍在開發中,而 TLS 1.3 尚未最終確定。 TLS 1.3 握手和協議行為與 TLS 1.2 及更早的版本略有差異,請參閱 [TLS 1.3](../library/ssl.xhtml#ssl-tlsv1-3)。 (由 Christian Heimes 在 [bpo-32947](https://bugs.python.org/issue32947) \[https://bugs.python.org/issue32947\], [bpo-20995](https://bugs.python.org/issue20995) \[https://bugs.python.org/issue20995\], [bpo-29136](https://bugs.python.org/issue29136) \[https://bugs.python.org/issue29136\], [bpo-30622](https://bugs.python.org/issue30622) \[https://bugs.python.org/issue30622\] 以及 [bpo-33618](https://bugs.python.org/issue33618) \[https://bugs.python.org/issue33618\] 中貢獻。) [`SSLSocket`](../library/ssl.xhtml#ssl.SSLSocket "ssl.SSLSocket") 和 [`SSLObject`](../library/ssl.xhtml#ssl.SSLObject "ssl.SSLObject") 不再具有公共構造器。 直接實例化從未成為有文檔和受支持的特性。 實際必須通過 [`SSLContext`](../library/ssl.xhtml#ssl.SSLContext "ssl.SSLContext") 的方法 [`wrap_socket()`](../library/ssl.xhtml#ssl.SSLContext.wrap_socket "ssl.SSLContext.wrap_socket") 和 [`wrap_bio()`](../library/ssl.xhtml#ssl.SSLContext.wrap_bio "ssl.SSLContext.wrap_bio") 來創建。 (由 Christian Heimes 在 [bpo-32951](https://bugs.python.org/issue32951) \[https://bugs.python.org/issue32951\] 中貢獻。) 用于設置最小和最大 TLS 協議版本的 OpenSSL 1.1 API 現已可用,名稱分別為 [`SSLContext.minimum_version`](../library/ssl.xhtml#ssl.SSLContext.minimum_version "ssl.SSLContext.minimum_version") 和 [`SSLContext.maximum_version`](../library/ssl.xhtml#ssl.SSLContext.maximum_version "ssl.SSLContext.maximum_version")。 受支持的協議由幾個新增旗標指定,例如 [`HAS_TLSv1_1`](../library/ssl.xhtml#ssl.HAS_TLSv1_1 "ssl.HAS_TLSv1_1")。 (由 Christian Heimes 在 [bpo-32609](https://bugs.python.org/issue32609) \[https://bugs.python.org/issue32609\] 中貢獻。) 增加了 `SSLContext.post_handshake_auth` 以及 [`ssl.SSLSocket.verify_client_post_handshake()`](../library/ssl.xhtml#ssl.SSLSocket.verify_client_post_handshake "ssl.SSLSocket.verify_client_post_handshake") 來啟用并初始化 TLS 1.3 握手后驗證。 (由 Christian Heimes 在 [bpo-34670](https://bugs.python.org/issue34670) \[https://bugs.python.org/issue34670\] 中貢獻。) ### string [`string.Template`](../library/string.xhtml#string.Template "string.Template") 現在允許你有選擇地分別修改帶大括號的占位符和不帶大括號的占位符所對應的正則表達式模式。 (由 Barry Warsaw 在 [bpo-1198569](https://bugs.python.org/issue1198569) \[https://bugs.python.org/issue1198569\] 中貢獻。) ### subprocess [`subprocess.run()`](../library/subprocess.xhtml#subprocess.run "subprocess.run") 函數接受新的 *capture\_output* 關鍵字參數。 當其為真值時,將會捕獲 stdout 和 stderr。 這相當于將 [`subprocess.PIPE`](../library/subprocess.xhtml#subprocess.PIPE "subprocess.PIPE") 作為 *stdout* 和 *stderr* 參數傳入。 (由 Bo Bayles 在 [bpo-32102](https://bugs.python.org/issue32102) \[https://bugs.python.org/issue32102\] 中貢獻。) `subprocess.run` 函數和 [`subprocess.Popen`](../library/subprocess.xhtml#subprocess.Popen "subprocess.Popen") 構造器現在接受 *text* 關鍵字參數作為 *universal\_newlines* 的別名。 (由 Andrew Clegg 在 [bpo-31756](https://bugs.python.org/issue31756) \[https://bugs.python.org/issue31756\] 中貢獻。) 在 Windows 中當重定向標準句柄時 *close\_fds* 的默認值由 `False` 改為 `True`。 現在可以在重定向標準句柄時將 *close\_fds* 設為真值。 參閱 [`subprocess.Popen`](../library/subprocess.xhtml#subprocess.Popen "subprocess.Popen")。 這意味著現在 *close\_fds* 在所有受支持的平臺上默認值均為 `True`。 (由 Segev Finer 在 [bpo-19764](https://bugs.python.org/issue19764) \[https://bugs.python.org/issue19764\] 中貢獻。) 在 [`subprocess.call()`](../library/subprocess.xhtml#subprocess.call "subprocess.call"), [`subprocess.run()`](../library/subprocess.xhtml#subprocess.run "subprocess.run") 期間或在 [`Popen`](../library/subprocess.xhtml#subprocess.Popen "subprocess.Popen") 上下文管理器中,subprocess 模塊現在能更優雅地處理 [`KeyboardInterrupt`](../library/exceptions.xhtml#KeyboardInterrupt "KeyboardInterrupt")。 它現在會等待一小段時間以便子進程退出,然后再繼續處理 `KeyboardInterrupt` 異常。 (由 Gregory P. Smith 在 [bpo-25942](https://bugs.python.org/issue25942) \[https://bugs.python.org/issue25942\] 中貢獻。) ### sys 新增 [`sys.breakpointhook()`](../library/sys.xhtml#sys.breakpointhook "sys.breakpointhook") 鉤子函數,供內置的 [`breakpoint()`](../library/functions.xhtml#breakpoint "breakpoint") 進行調用。 (由 Barry Warsaw 在 [bpo-31353](https://bugs.python.org/issue31353) \[https://bugs.python.org/issue31353\] 中貢獻。) 在 Android 中新增的 [`sys.getandroidapilevel()`](../library/sys.xhtml#sys.getandroidapilevel "sys.getandroidapilevel") 會返回構建時的 Android API 版本。 (由 Victor Stinner 在 [bpo-28740](https://bugs.python.org/issue28740) \[https://bugs.python.org/issue28740\] 中貢獻。) 新的 [`sys.get_coroutine_origin_tracking_depth()`](../library/sys.xhtml#sys.get_coroutine_origin_tracking_depth "sys.get_coroutine_origin_tracking_depth") 函數會返回當前協程的由新的 [`sys.set_coroutine_origin_tracking_depth()`](../library/sys.xhtml#sys.set_coroutine_origin_tracking_depth "sys.set_coroutine_origin_tracking_depth") 所設定的原始跟蹤深度。 [`asyncio`](../library/asyncio.xhtml#module-asyncio "asyncio: Asynchronous I/O.") 已轉換為使用這個新 API 代替已棄用的 [`sys.set_coroutine_wrapper()`](../library/sys.xhtml#sys.set_coroutine_wrapper "sys.set_coroutine_wrapper")。 (由 Nathaniel J. Smith 在 [bpo-32591](https://bugs.python.org/issue32591) \[https://bugs.python.org/issue32591\] 中貢獻。) ### time [**PEP 564**](https://www.python.org/dev/peps/pep-0564) \[https://www.python.org/dev/peps/pep-0564\] 向 [`time`](../library/time.xhtml#module-time "time: Time access and conversions.") 模塊增加六個具有納秒級精度的新函數: - [`time.clock_gettime_ns()`](../library/time.xhtml#time.clock_gettime_ns "time.clock_gettime_ns") - [`time.clock_settime_ns()`](../library/time.xhtml#time.clock_settime_ns "time.clock_settime_ns") - [`time.monotonic_ns()`](../library/time.xhtml#time.monotonic_ns "time.monotonic_ns") - [`time.perf_counter_ns()`](../library/time.xhtml#time.perf_counter_ns "time.perf_counter_ns") - [`time.process_time_ns()`](../library/time.xhtml#time.process_time_ns "time.process_time_ns") - [`time.time_ns()`](../library/time.xhtml#time.time_ns "time.time_ns") 增加了新的時鐘標識符: - [`time.CLOCK_BOOTTIME`](../library/time.xhtml#time.CLOCK_BOOTTIME "time.CLOCK_BOOTTIME") (Linux): 與 [`time.CLOCK_MONOTONIC`](../library/time.xhtml#time.CLOCK_MONOTONIC "time.CLOCK_MONOTONIC") 相似,不同點在于它還包括任何系統掛起的時間。 - [`time.CLOCK_PROF`](../library/time.xhtml#time.CLOCK_PROF "time.CLOCK_PROF") (FreeBSD, NetBSD 和 OpenBSD): 高精度的分進程 CPU 計時器。 - [`time.CLOCK_UPTIME`](../library/time.xhtml#time.CLOCK_UPTIME "time.CLOCK_UPTIME") (FreeBSD, OpenBSD): 該時間的絕對值是系統運行且未掛起的時間,提供準確的正常運行時間度量。 新的 [`time.thread_time()`](../library/time.xhtml#time.thread_time "time.thread_time") 和 [`time.thread_time_ns()`](../library/time.xhtml#time.thread_time_ns "time.thread_time_ns") 函數可用于獲取每線程的 CPU 時間度量。 (由 Antoine Pitrou 在 [bpo-32025](https://bugs.python.org/issue32025) \[https://bugs.python.org/issue32025\] 中貢獻。) 新的 [`time.pthread_getcpuclockid()`](../library/time.xhtml#time.pthread_getcpuclockid "time.pthread_getcpuclockid") 函數會返回特定線程中 CPU 時鐘的時鐘 ID。 ### tkinter 新的 [`tkinter.ttk.Spinbox`](../library/tkinter.ttk.xhtml#tkinter.ttk.Spinbox "tkinter.ttk.Spinbox") 類現已可用。 (由 Alan Moore 在 [bpo-32585](https://bugs.python.org/issue32585) \[https://bugs.python.org/issue32585\] 中貢獻。) ### tracemalloc [`tracemalloc.Traceback`](../library/tracemalloc.xhtml#tracemalloc.Traceback "tracemalloc.Traceback") 的行為更接近正規的回溯,會對所有幀按從最舊到最新來排序。 [`Traceback.format()`](../library/tracemalloc.xhtml#tracemalloc.Traceback.format "tracemalloc.Traceback.format") 現在接受負的 *limit*,并會將結果截短至排在第 `abs(limit)` 位的舊幀。 如果要獲得舊的行為,請在 `Traceback.format()` 中使用新的 *most\_recent\_first* 參數。 (由 Jesse Bakker 在 [bpo-32121](https://bugs.python.org/issue32121) \[https://bugs.python.org/issue32121\] 中貢獻。) ### types 新的 [`WrapperDescriptorType`](../library/types.xhtml#types.WrapperDescriptorType "types.WrapperDescriptorType"), [`MethodWrapperType`](../library/types.xhtml#types.MethodWrapperType "types.MethodWrapperType"), [`MethodDescriptorType`](../library/types.xhtml#types.MethodDescriptorType "types.MethodDescriptorType") 和 [`ClassMethodDescriptorType`](../library/types.xhtml#types.ClassMethodDescriptorType "types.ClassMethodDescriptorType") 類現已可用。 (由 Manuel Krebber 和 Guido van Rossum 在 [bpo-29377](https://bugs.python.org/issue29377) \[https://bugs.python.org/issue29377\] 以及 Serhiy Storchaka 在 [bpo-32265](https://bugs.python.org/issue32265) \[https://bugs.python.org/issue32265\] 中貢獻。) 新的 [`types.resolve_bases()`](../library/types.xhtml#types.resolve_bases "types.resolve_bases") 函數會以 [**PEP 560**](https://www.python.org/dev/peps/pep-0560) \[https://www.python.org/dev/peps/pep-0560\] 所規定的方式動態解析 MRO 條目。 (由 Ivan Levkivskyi 在 [bpo-32717](https://bugs.python.org/issue32717) \[https://bugs.python.org/issue32717\] 中貢獻。) ### unicodedata 內部的 [`unicodedata`](../library/unicodedata.xhtml#module-unicodedata "unicodedata: Access the Unicode Database.") 數據庫已升級為使用 [Unicode 11](http://www.unicode.org/versions/Unicode11.0.0/) \[http://www.unicode.org/versions/Unicode11.0.0/\]。 (由 Benjamin Peterson 貢獻。) ### unittest 新的 `-k` 命令行選項允許通過名稱子字符串或類似于 Unix shell 的模式來篩選測試項。 例如,`python -m unittest -k foo` 將運行 `foo_tests.SomeTest.test_something`, `bar_tests.SomeTest.test_foo`,但不會運行 `bar_tests.FooTest.test_something`。 (由 Jonas Haag 在 [bpo-32071](https://bugs.python.org/issue32071) \[https://bugs.python.org/issue32071\] 中貢獻。) ### unittest.mock 現在 [`sentinel`](../library/unittest.mock.xhtml#unittest.mock.sentinel "unittest.mock.sentinel") 屬性會在它們被 [`復制`](../library/copy.xhtml#module-copy "copy: Shallow and deep copy operations.") 或 [`封存`](../library/pickle.xhtml#module-pickle "pickle: Convert Python objects to streams of bytes and back.") 時保存其標識。 (由 Serhiy Storchaka 在 [bpo-20804](https://bugs.python.org/issue20804) \[https://bugs.python.org/issue20804\] 中貢獻。) 新的 [`seal()`](../library/unittest.mock.xhtml#unittest.mock.seal "unittest.mock.seal") 函數允許 [`Mock`](../library/unittest.mock.xhtml#unittest.mock.Mock "unittest.mock.Mock") 對實例進行密封,這將禁止進一步創建屬性模擬。 密封會以遞歸方式應用于自身模擬的所有屬性。 (由 Mario Corchero 在 [bpo-30541](https://bugs.python.org/issue30541) \[https://bugs.python.org/issue30541\] 中貢獻。) ### urllib.parse [`urllib.parse.quote()`](../library/urllib.parse.xhtml#urllib.parse.quote "urllib.parse.quote") 已經從 [**RFC 2396**](https://tools.ietf.org/html/rfc2396.html) \[https://tools.ietf.org/html/rfc2396.html\] 更新為 [**RFC 3986**](https://tools.ietf.org/html/rfc3986.html) \[https://tools.ietf.org/html/rfc3986.html\],將 `~` 添加到默認情況下從未引用的字符集。 (由 Christian Theune 和 Ratnadeep Debnath 在 [bpo-16285](https://bugs.python.org/issue16285) \[https://bugs.python.org/issue16285\] 中貢獻。) ### uu [`uu.encode()`](../library/uu.xhtml#uu.encode "uu.encode") 函數現在接受可選的 *backtick* 關鍵字參數。 當其為真時,零會以 `'`'` 而非空格來表示。 (由 Xiang Zhang 在 [bpo-30103](https://bugs.python.org/issue30103) \[https://bugs.python.org/issue30103\] 中貢獻。) ### uuid 新的 [`UUID.is_safe`](../library/uuid.xhtml#uuid.UUID.is_safe "uuid.UUID.is_safe") 屬性會從平臺中繼有關是否使用多進程安全模式來生成所需 UUID 的信息。 (由 Barry Warsaw 在 [bpo-22807](https://bugs.python.org/issue22807) \[https://bugs.python.org/issue22807\] 中貢獻。) [`uuid.getnode()`](../library/uuid.xhtml#uuid.getnode "uuid.getnode") 現在更傾向于統一管理的 MAC 地址而不是本地管理的 MAC 地址。 這樣可以更好地保證從 [`uuid.uuid1()`](../library/uuid.xhtml#uuid.uuid1 "uuid.uuid1") 返回的 UUID 的全局唯一性。 如果只有本地管理的 MAC 地址可用,則返回首個找到的此類地址。 (由 Barry Warsaw 在 [bpo-32107](https://bugs.python.org/issue32107) \[https://bugs.python.org/issue32107\] 中貢獻。) ### warnings 默認警告過濾器的初始化已進行以下更改: - 通過命令行選項(包括 [`-b`](../using/cmdline.xhtml#cmdoption-b) 以及新的 CPython 專屬的 [`-X`](../using/cmdline.xhtml#id5)`dev` 選項)啟用的警告總是會通過 [`sys.warnoptions`](../library/sys.xhtml#sys.warnoptions "sys.warnoptions") 屬性被傳遞給警告機制。 - 通過命令行或環境變量啟用的警告過濾器現在具有以下優先順序: > - 用于 [`-b`](../using/cmdline.xhtml#cmdoption-b) (或 `-bb`) 的 `BytesWarning` 過濾器 > - 通過 [`-W`](../using/cmdline.xhtml#cmdoption-w) 選項指定的任何過濾器 > - 通過 [`PYTHONWARNINGS`](../using/cmdline.xhtml#envvar-PYTHONWARNINGS) 環境變量指定的任何過濾器 > - 任何其他 CPython 專屬過濾器(例如 `-X dev` 模式中新增的 `default` 過濾器) > - 由警告機制所定義的任何隱式過濾器 - 在 CPython 調試版本中,現在默認情況下會顯示所有警告(隱式過濾器列表為空) (由 Nick Coghlan 和 Victor Stinner 在 [bpo-20361](https://bugs.python.org/issue20361) \[https://bugs.python.org/issue20361\], [bpo-32043](https://bugs.python.org/issue32043) \[https://bugs.python.org/issue32043\] 以及 [bpo-32230](https://bugs.python.org/issue32230) \[https://bugs.python.org/issue32230\] 中貢獻。) 在單文件腳本和交互式提示符中,默認情況下會再次顯示已棄用警告。 詳情參見 [PEP 565: 在 \_\_main\_\_ 中顯示 DeprecationWarning](#whatsnew37-pep565)。 (由 Nick Coghlan 在 [bpo-31975](https://bugs.python.org/issue31975) \[https://bugs.python.org/issue31975\] 中貢獻。) ### xml 作為對 DTD 和外部實體檢索的緩解,在默認情況下 [`xml.dom.minidom`](../library/xml.dom.minidom.xhtml#module-xml.dom.minidom "xml.dom.minidom: Minimal Document Object Model (DOM) implementation.") 和 [`xml.sax`](../library/xml.sax.xhtml#module-xml.sax "xml.sax: Package containing SAX2 base classes and convenience functions.") 模塊不再處理外部實體。 (由 Christian Heimes 在 [bpo-17239](https://bugs.python.org/issue17239) \[https://bugs.python.org/issue17239\] 中貢獻。) ### xml.etree `find()` 方法中的 [ElementPath](../library/xml.etree.elementtree.xhtml#elementtree-xpath) 描述詞現在可以將當前節點文本與 `[. = "text"]` 進行比較,而不僅是子節點中的文本。 描述詞還允許添加空格以提高可讀性。 (由 Stefan Behnel 在 [bpo-31648](https://bugs.python.org/issue31648) \[https://bugs.python.org/issue31648\] 中貢獻。) ### xmlrpc.server `SimpleXMLRPCDispatcher.register_function` 現在可以被用作裝飾器。 (由 Xiang Zhang 在 [bpo-7769](https://bugs.python.org/issue7769) \[https://bugs.python.org/issue7769\] 中貢獻。) ### zipapp 函數 [`create_archive()`](../library/zipapp.xhtml#zipapp.create_archive "zipapp.create_archive") 現在接受可選的 *filter* 參數,以允許用戶選擇哪些文件應被加入歸檔包。 (由 Irmen de Jong 在 [bpo-31072](https://bugs.python.org/issue31072) \[https://bugs.python.org/issue31072\] 中貢獻。) 函數 [`create_archive()`](../library/zipapp.xhtml#zipapp.create_archive "zipapp.create_archive") 現在接受可選的 *compressed* 參數,以生成壓縮歸檔包。 另外也加入了命令行選項 `--compress` 以支持壓縮。 (由 Zhiming Wang 在 [bpo-31638](https://bugs.python.org/issue31638) \[https://bugs.python.org/issue31638\] 中貢獻。) ### zipfile [`ZipFile`](../library/zipfile.xhtml#zipfile.ZipFile "zipfile.ZipFile") 現在接受新的 *compresslevel* 形參,以控制壓縮級別。 (由 Bo Bayles 在 [bpo-21417](https://bugs.python.org/issue21417) \[https://bugs.python.org/issue21417\] 中貢獻。) `ZipFile` 所創建的歸檔包中的子目錄現在會按字母表順序保存。 (由 Bernhard M. Wiedemann 在 [bpo-30693](https://bugs.python.org/issue30693) \[https://bugs.python.org/issue30693\] 中貢獻。) ## C API 的改變 已實現了用于線程本地存儲的新 API。 相關概述請參閱 [PEP 539: 用于線程局部存儲的新 C API](#whatsnew37-pep539),完整參考文檔請查看 [Thread Specific Storage (TSS) API](../c-api/init.xhtml#thread-specific-storage-api)。 (由 Masayuki Yamamoto 在 [bpo-25658](https://bugs.python.org/issue25658) \[https://bugs.python.org/issue25658\] 中貢獻。) 新的 [上下文變量](#whatsnew37-pep567) 功能開放了許多 [新的 C API](../c-api/contextvars.xhtml#contextvarsobjects)。 新的 [`PyImport_GetModule()`](../c-api/import.xhtml#c.PyImport_GetModule "PyImport_GetModule") 函數會返回之前所導入的具有給定名稱的模塊。 (由 Eric Snow 在 [bpo-28411](https://bugs.python.org/issue28411) \[https://bugs.python.org/issue28411\] 中貢獻。) 新的 [`Py_RETURN_RICHCOMPARE`](../c-api/typeobj.xhtml#c.Py_RETURN_RICHCOMPARE "Py_RETURN_RICHCOMPARE") 宏可以簡化豐富比較函數的編寫。 (由 Petr Victorin 在 [bpo-23699](https://bugs.python.org/issue23699) \[https://bugs.python.org/issue23699\] 中貢獻。) 新的 [`Py_UNREACHABLE`](../c-api/intro.xhtml#c.Py_UNREACHABLE "Py_UNREACHABLE") 宏可用于標記不可到達的代碼路徑。 (由 Barry Warsaw 在 [bpo-31338](https://bugs.python.org/issue31338) \[https://bugs.python.org/issue31338\] 中貢獻。) [`tracemalloc`](../library/tracemalloc.xhtml#module-tracemalloc "tracemalloc: Trace memory allocations.") 現在通過新的 `PyTraceMalloc_Track()` 和 `PyTraceMalloc_Untrack()` 函數公開了一個 C API。 (由 Victor Stinner 在 [bpo-30054](https://bugs.python.org/issue30054) \[https://bugs.python.org/issue30054\] 中貢獻。) 新的 [`import__find__load__start()`](../howto/instrumentation.xhtml#c.import__find__load__start "import__find__load__start") 和 [`import__find__load__done()`](../howto/instrumentation.xhtml#c.import__find__load__done "import__find__load__done") 靜態標記可用于跟蹤模塊導入。 (由 Christian Heimes 在 [bpo-31574](https://bugs.python.org/issue31574) \[https://bugs.python.org/issue31574\] 中貢獻。) 結構體 [`PyMemberDef`](../c-api/structures.xhtml#c.PyMemberDef "PyMemberDef"), [`PyGetSetDef`](../c-api/structures.xhtml#c.PyGetSetDef "PyGetSetDef"), [`PyStructSequence_Field`](../c-api/tuple.xhtml#c.PyStructSequence_Field "PyStructSequence_Field"), [`PyStructSequence_Desc`](../c-api/tuple.xhtml#c.PyStructSequence_Desc "PyStructSequence_Desc") 和 `wrapperbase` 的字段 `name` 和 `doc` 現在的類型為 `const char *` 而不是 `char *`。 (由 Serhiy Storchaka 在 [bpo-28761](https://bugs.python.org/issue28761) \[https://bugs.python.org/issue28761\] 中貢獻。) [`PyUnicode_AsUTF8AndSize()`](../c-api/unicode.xhtml#c.PyUnicode_AsUTF8AndSize "PyUnicode_AsUTF8AndSize") 和 [`PyUnicode_AsUTF8()`](../c-api/unicode.xhtml#c.PyUnicode_AsUTF8 "PyUnicode_AsUTF8") 的結果類型現在是 `const char *` 而非 `char *`。 (由 Serhiy Storchaka 在 [bpo-28769](https://bugs.python.org/issue28769) \[https://bugs.python.org/issue28769\] 中貢獻。) [`PyMapping_Keys()`](../c-api/mapping.xhtml#c.PyMapping_Keys "PyMapping_Keys"), [`PyMapping_Values()`](../c-api/mapping.xhtml#c.PyMapping_Values "PyMapping_Values") 和 [`PyMapping_Items()`](../c-api/mapping.xhtml#c.PyMapping_Items "PyMapping_Items") 的結果現在肯定是列表,而非可能是列表也可能是元組。 (由 Oren Milman 在 [bpo-28280](https://bugs.python.org/issue28280) \[https://bugs.python.org/issue28280\] 中貢獻。) 添加了函數 [`PySlice_Unpack()`](../c-api/slice.xhtml#c.PySlice_Unpack "PySlice_Unpack") 和 [`PySlice_AdjustIndices()`](../c-api/slice.xhtml#c.PySlice_AdjustIndices "PySlice_AdjustIndices")。 (由 Serhiy Storchaka 在 [bpo-27867](https://bugs.python.org/issue27867) \[https://bugs.python.org/issue27867\] 中貢獻。) [`PyOS_AfterFork()`](../c-api/sys.xhtml#c.PyOS_AfterFork "PyOS_AfterFork") 已棄用,建議改用新的 functions [`PyOS_BeforeFork()`](../c-api/sys.xhtml#c.PyOS_BeforeFork "PyOS_BeforeFork"), [`PyOS_AfterFork_Parent()`](../c-api/sys.xhtml#c.PyOS_AfterFork_Parent "PyOS_AfterFork_Parent") 和 [`PyOS_AfterFork_Child()`](../c-api/sys.xhtml#c.PyOS_AfterFork_Child "PyOS_AfterFork_Child")。 (由 Antoine Pitrou 在 [bpo-16500](https://bugs.python.org/issue16500) \[https://bugs.python.org/issue16500\] 中貢獻。) 曾經作為公共 API 一部分的 `PyExc_RecursionErrorInst` 單例已被移除,因為它的成員永遠不會被清理,可能在解釋器的最終化過程中導致段錯誤。 由 Xavier de Gaye 在 [bpo-22898](https://bugs.python.org/issue22898) \[https://bugs.python.org/issue22898\] 和 [bpo-30697](https://bugs.python.org/issue30697) \[https://bugs.python.org/issue30697\] 中貢獻。 添加 C API 對使用 timezone 的構造器 [`PyTimeZone_FromOffset()`](../c-api/datetime.xhtml#c.PyTimeZone_FromOffset "PyTimeZone_FromOffset") 和 [`PyTimeZone_FromOffsetAndName()`](../c-api/datetime.xhtml#c.PyTimeZone_FromOffsetAndName "PyTimeZone_FromOffsetAndName") 的時區的支持,以及通常 [`PyDateTime_TimeZone_UTC`](../c-api/datetime.xhtml#c.PyDateTime_TimeZone_UTC "PyDateTime_TimeZone_UTC") 使用 UTC 單例。 由 Paul Ganssle 在 [bpo-10381](https://bugs.python.org/issue10381) \[https://bugs.python.org/issue10381\] 中貢獻。 `PyThread_start_new_thread()` 和 `PyThread_get_thread_ident()` 的結果類型以及 [`PyThreadState_SetAsyncExc()`](../c-api/init.xhtml#c.PyThreadState_SetAsyncExc "PyThreadState_SetAsyncExc") 的 *id* 參數類型由 `long` 改為 `unsigned long`。 (由 Serhiy Storchaka 在 [bpo-6532](https://bugs.python.org/issue6532) \[https://bugs.python.org/issue6532\] 中貢獻。) 現在如果第二個參數為 *NULL* 并且 `wchar_t*` 字符串包含空字符 [`PyUnicode_AsWideCharString()`](../c-api/unicode.xhtml#c.PyUnicode_AsWideCharString "PyUnicode_AsWideCharString") 將引發 [`ValueError`](../library/exceptions.xhtml#ValueError "ValueError")。 (由 Serhiy Storchaka 在 [bpo-30708](https://bugs.python.org/issue30708) \[https://bugs.python.org/issue30708\] 中貢獻。) 對啟動順序以及動態內存分配器管理的更改意味著早已記錄在案的,對在調用大多數 C API 函數之前調用 [`Py_Initialize()`](../c-api/init.xhtml#c.Py_Initialize "Py_Initialize") 的要求的依賴現在變得更加強烈,未遵循此要求可能導致嵌入式應用程序中的段錯誤。 請參閱此文檔的 [移植到 Python 3.7](#porting-to-python-37) 一節以及 C API 文檔的 [在Python初始化之前](../c-api/init.xhtml#pre-init-safe) 一節了解更多細節。 新的 [`PyInterpreterState_GetID()`](../c-api/init.xhtml#c.PyInterpreterState_GetID "PyInterpreterState_GetID") 會返回給定解釋器的唯一 ID。 (由 Eric Snow 在 [bpo-29102](https://bugs.python.org/issue29102) \[https://bugs.python.org/issue29102\] 中貢獻。) 現在當啟用 [UTF-8 模式](#whatsnew37-pep540) 時 [`Py_DecodeLocale()`](../c-api/sys.xhtml#c.Py_DecodeLocale "Py_DecodeLocale"), [`Py_EncodeLocale()`](../c-api/sys.xhtml#c.Py_EncodeLocale "Py_EncodeLocale") 會使用 UTF-8 編碼。 (由 Victor Stinner 在 [bpo-29240](https://bugs.python.org/issue29240) \[https://bugs.python.org/issue29240\] 中貢獻。) [`PyUnicode_DecodeLocaleAndSize()`](../c-api/unicode.xhtml#c.PyUnicode_DecodeLocaleAndSize "PyUnicode_DecodeLocaleAndSize") 和 [`PyUnicode_EncodeLocale()`](../c-api/unicode.xhtml#c.PyUnicode_EncodeLocale "PyUnicode_EncodeLocale") 現在會為 `surrogateescape` 錯誤句柄使用當前區域編碼。 (由 Victor Stinner 在 [bpo-29240](https://bugs.python.org/issue29240) \[https://bugs.python.org/issue29240\] 中貢獻。) [`PyUnicode_FindChar()`](../c-api/unicode.xhtml#c.PyUnicode_FindChar "PyUnicode_FindChar") 的 *start* 和 *end* 形參的行為現在調整為與字符串切片類似。 (由 Xiang Zhang 在 [bpo-28822](https://bugs.python.org/issue28822) \[https://bugs.python.org/issue28822\] 中貢獻。) ## 構建的改變 對于 `--without-threads` 構建的支持已被移除。 [`threading`](../library/threading.xhtml#module-threading "threading: Thread-based parallelism.") 模塊現在將總是可用。 (由 Antoine Pitrou 在 [bpo-31370](https://bugs.python.org/issue31370) \[https://bugs.python.org/issue31370\] 中貢獻。) 在非 OSX UNIX 平臺上已不再包含用于構建 [`_ctypes`](../library/ctypes.xhtml#module-ctypes "ctypes: A foreign function library for Python.") 模塊的完整 libffi 副本。 現在當在此類平臺上構建 `_ctypes` 時需要事先裝有 libffi 的副本。 (由 Zachary Ware 在 [bpo-27979](https://bugs.python.org/issue27979) \[https://bugs.python.org/issue27979\] 中貢獻。) Windows 構建過程不再依賴 Subversion 來拉取外部源碼,而是改用一段 Python 腳本從 GitHub 下載 zip 文件。 如果未在系統中找到 Python 3.6 (通過 `py -3.6`),則會使用 NuGet 下載一份 32 位的 Python 副本用于此目的。 (由 Zachary Ware 在 [bpo-30450](https://bugs.python.org/issue30450) \[https://bugs.python.org/issue30450\] 中貢獻。) [`ssl`](../library/ssl.xhtml#module-ssl "ssl: TLS/SSL wrapper for socket objects") 模塊需要兼容 OpenSSL 1.0.2 或 1.1 的 libssl。 OpenSSL 1.0.1 的生命期已于 2016-12-31 終止且不再受支持。 LibreSSL 暫時也不受支持。 LibreSSL 發布版直到 2.6.4 版還缺少所需的 OpenSSL 1.0.2 API。 ## 性能優化 通過移植更多代碼來使用 `METH_FASTCALL` 的約定,可以顯著地減少調用 C 代碼中實現的各類標準庫的很多方法的開銷。 (由 Victor Stinner 在 [bpo-29300](https://bugs.python.org/issue29300) \[https://bugs.python.org/issue29300\]、[bpo-29507](https://bugs.python.org/issue29507) \[https://bugs.python.org/issue29507\]、[bpo-29452](https://bugs.python.org/issue29452) \[https://bugs.python.org/issue29452\] 以及 [bpo-29286](https://bugs.python.org/issue29286) \[https://bugs.python.org/issue29286\] 中貢獻。) 通過各種優化方式,使 Python 在 Linux 上的啟動時間縮短了 10%,在 macOS 上縮短了 30%。 (由 Victor Stinner, INADA Naoki 在 [bpo-29585](https://bugs.python.org/issue29585) \[https://bugs.python.org/issue29585\] 中,以及 Ivan Levkivskyi 在 [bpo-31333](https://bugs.python.org/issue31333) \[https://bugs.python.org/issue31333\] 中貢獻。) 由于避免創建綁定方法案例的字節碼更改,方法調用速度現在加快了 20%。 (由 Yury Selivanov 和 INADA Naoki 在 [bpo-26110](https://bugs.python.org/issue26110) \[https://bugs.python.org/issue26110\] 中貢獻。) 對 [`asyncio`](../library/asyncio.xhtml#module-asyncio "asyncio: Asynchronous I/O.") 模塊里面的一些常用函數做了顯著的性能優化。 - [`asyncio.get_event_loop()`](../library/asyncio-eventloop.xhtml#asyncio.get_event_loop "asyncio.get_event_loop") 函數已經改用 C 重新實現,使其執行速度加快了 15 倍。 (由 Yury Selivanov 在 [bpo-32296](https://bugs.python.org/issue32296) \[https://bugs.python.org/issue32296\] 中貢獻。) - [`asyncio.Future`](../library/asyncio-future.xhtml#asyncio.Future "asyncio.Future") 回調管理已經過優化。 (由 Yury Selivanov 在 [bpo-32348](https://bugs.python.org/issue32348) \[https://bugs.python.org/issue32348\] 中貢獻。) - [`asyncio.gather()`](../library/asyncio-task.xhtml#asyncio.gather "asyncio.gather") 的執行速度現在加快了 15%。 (由 Yury Selivanov 在 [bpo-32355](https://bugs.python.org/issue32355) \[https://bugs.python.org/issue32355\] 中貢獻。) - 當 *delay* 參數為零或負值時 [`asyncio.sleep()`](../library/asyncio-task.xhtml#asyncio.sleep "asyncio.sleep") 的執行速度現在加快了 2 倍。 (由 Andrew Svetlov 在 [bpo-32351](https://bugs.python.org/issue32351) \[https://bugs.python.org/issue32351\] 中貢獻。) - asyncio 調試模式的執行開銷已獲減輕。 (由 Antoine Pitrou 在 [bpo-31970](https://bugs.python.org/issue31970) \[https://bugs.python.org/issue31970\] 中貢獻。) 作為 [PEP 560 工作](#whatsnew37-pep560) 的結果,[`typing`](../library/typing.xhtml#module-typing "typing: Support for type hints (see PEP 484).") 的導入時間已減少了 7 倍,許多與類型相關的操作現在會執行得更快。 (由 Ivan Levkivskyi 在 [bpo-32226](https://bugs.python.org/issue32226) \[https://bugs.python.org/issue32226\] 中貢獻。) [`sorted()`](../library/functions.xhtml#sorted "sorted") 和 [`list.sort()`](../library/stdtypes.xhtml#list.sort "list.sort") 已經過優化,在通常情況下執行速度可提升 40-75%。 (由 Elliot Gorokhovsky 在 [bpo-28685](https://bugs.python.org/issue28685) \[https://bugs.python.org/issue28685\] 中貢獻。) [`dict.copy()`](../library/stdtypes.xhtml#dict.copy "dict.copy") 的執行速度現在加快了 5.5 倍。 (由 Yury Selivanov 在 [bpo-31179](https://bugs.python.org/issue31179) \[https://bugs.python.org/issue31179\] 中貢獻。) 當 *name* 未找到并且 *obj* 未重載 [`object.__getattr__()`](../reference/datamodel.xhtml#object.__getattr__ "object.__getattr__") 或 [`object.__getattribute__()`](../reference/datamodel.xhtml#object.__getattribute__ "object.__getattribute__") 時 [`hasattr()`](../library/functions.xhtml#hasattr "hasattr") 和 [`getattr()`](../library/functions.xhtml#getattr "getattr") 現在會比原來快大約 4 倍。 (由 INADA Naoki 在 [bpo-32544](https://bugs.python.org/issue32544) \[https://bugs.python.org/issue32544\] 中貢獻。) 在字符串中搜索特定的 Unicode 字符(例如烏克蘭語字母“?”)會比搜索其他字符慢上 25 倍。 而現在最壞情況下也只會慢上 3 倍。 (由 Serhiy Storchaka 在 [bpo-24821](https://bugs.python.org/issue24821) \[https://bugs.python.org/issue24821\] 中貢獻。) [`collections.namedtuple()`](../library/collections.xhtml#collections.namedtuple "collections.namedtuple") 工廠對象已經重寫實現,使得創建具名元組的速度加快了 4 到 6 倍。 (由 Jelle Zijlstra 在 [bpo-28638](https://bugs.python.org/issue28638) \[https://bugs.python.org/issue28638\] 中貢獻,進一步的改進由 INADA Naoki, Serhiy Storchaka 和 Raymond Hettinger 貢獻。) 現在 `date.fromordinal()` 和 `date.fromtimestamp()` 在通常情況下執行速度可提升 30%。 (由 Paul Ganssle 在 [bpo-32403](https://bugs.python.org/issue32403) \[https://bugs.python.org/issue32403\] 中貢獻。) 由于使用了 [`os.scandir()`](../library/os.xhtml#os.scandir "os.scandir"),現在 [`os.fwalk()`](../library/os.xhtml#os.fwalk "os.fwalk") 函數執行速度提升了 2 倍。 (由 Serhiy Storchaka 在 [bpo-25996](https://bugs.python.org/issue25996) \[https://bugs.python.org/issue25996\] 中貢獻。) 由于使用了 [`os.scandir()`](../library/os.xhtml#os.scandir "os.scandir") 函數,[`shutil.rmtree()`](../library/shutil.xhtml#shutil.rmtree "shutil.rmtree") 函數的執行速度已經提升了 20--40%。 (由 Serhiy Storchaka 在 [bpo-28564](https://bugs.python.org/issue28564) \[https://bugs.python.org/issue28564\] 中貢獻。) [`正則表達式`](../library/re.xhtml#module-re "re: Regular expression operations.") 忽略大小寫的匹配和搜索已獲得優化。 現在搜索某些模式的速度提升了 20 倍。 (由 Serhiy Storchaka 在 [bpo-30285](https://bugs.python.org/issue30285) \[https://bugs.python.org/issue30285\] 中貢獻。) [`re.compile()`](../library/re.xhtml#re.compile "re.compile") 現在會將 `flags` 形參轉換為 int 對象,如果它是 `RegexFlag` 的話。 它現在會和 Python 3.5 一樣快,而比 Python 3.6 快大約 10%,實際速度取決于具體的模式。 (由 INADA Naoki 在 [bpo-31671](https://bugs.python.org/issue31671) \[https://bugs.python.org/issue31671\] 中貢獻。) [`selectors.EpollSelector`](../library/selectors.xhtml#selectors.EpollSelector "selectors.EpollSelector"), [`selectors.PollSelector`](../library/selectors.xhtml#selectors.PollSelector "selectors.PollSelector") 和 [`selectors.DevpollSelector`](../library/selectors.xhtml#selectors.DevpollSelector "selectors.DevpollSelector") 這幾個類的 [`modify()`](../library/selectors.xhtml#selectors.BaseSelector.modify "selectors.BaseSelector.modify") 方法在重負載下可以加快 10% 左右。 (由 Giampaolo Rodola' 在 [bpo-30014](https://bugs.python.org/issue30014) \[https://bugs.python.org/issue30014\] 中貢獻。) 常量折疊已經從窺孔優化器遷移至新的 AST 優化器,后者可以以更高的一致性來執行優化。 (由 Eugene Toder 和 INADA Naoki 在 [bpo-29469](https://bugs.python.org/issue29469) \[https://bugs.python.org/issue29469\] 和 [bpo-11549](https://bugs.python.org/issue11549) \[https://bugs.python.org/issue11549\] 中貢獻。) [`abc`](../library/abc.xhtml#module-abc "abc: Abstract base classes according to PEP 3119.") 中的大部分函數和方法已經用 C 重寫。 這使得創建抽像基類以及調用其 [`isinstance()`](../library/functions.xhtml#isinstance "isinstance") 和 [`issubclass()`](../library/functions.xhtml#issubclass "issubclass") 的速度加快了 1.5 倍。 這也使得 Python 啟動耗時減少了 10%。 (由 Ivan Levkivskyi 和 INADA Naoki 在 [bpo-31333](https://bugs.python.org/issue31333) \[https://bugs.python.org/issue31333\] 中貢獻。) 在不構造子類時,通過使用快速路徑構造器使得 [`datetime.date`](../library/datetime.xhtml#datetime.date "datetime.date") 和 [`datetime.datetime`](../library/datetime.xhtml#datetime.datetime "datetime.datetime") 的替代構造器獲得了顯著的速度提升。 (由 Paul Ganssle 在 [bpo-32403](https://bugs.python.org/issue32403) \[https://bugs.python.org/issue32403\] 中貢獻。) 在特定情況下 [`array.array`](../library/array.xhtml#array.array "array.array") 實例的比較速度已獲得很大提升。 現在當比較存放相同的整數類型的值的數組時會比原來快 10 到 70 倍。 (由 Adrian Wielgosik 在 [bpo-24700](https://bugs.python.org/issue24700) \[https://bugs.python.org/issue24700\] 中貢獻。) 在大多數平臺上 [`math.erf()`](../library/math.xhtml#math.erf "math.erf") 和 [`math.erfc()`](../library/math.xhtml#math.erfc "math.erfc") 函數現在使用(更快的)C 庫實現。 (由 Serhiy Storchaka 在 [bpo-26121](https://bugs.python.org/issue26121) \[https://bugs.python.org/issue26121\] 中貢獻。) ## 其他 CPython 實現的改變 - 跟蹤鉤子現在可以選擇不接收 `line` 而是選擇從解釋器接收 `opcode` 事件,具體做法是在被跟蹤的幀上相應地設置新的 `f_trace_lines` 和 `f_trace_opcodes` 屬性。 (由 Nick Coghlan 在 [bpo-31344](https://bugs.python.org/issue31344) \[https://bugs.python.org/issue31344\] 中貢獻。) - 修復了一些命名空間包模塊屬性的一致性問題。 命名空間模塊對象的 `__file__` 被設置為 `None` (原先未設置),對象的 `__spec__.origin` 也被設置為 `None` (之前為字符串 `"namespace"`)。 參見 [bpo-32305](https://bugs.python.org/issue32305) \[https://bugs.python.org/issue32305\]。 而且,命名空間模塊對象的 `__spec__.loader` 被設置的值與 `__loader__` 相同 (原先前者被設置為 `None`)。 參見 [bpo-32303](https://bugs.python.org/issue32303) \[https://bugs.python.org/issue32303\]。 - [`locals()`](../library/functions.xhtml#locals "locals") 字典現在以變量定義的詞法順序顯示。 原先未定義順序。 (由 Raymond Hettinger 在 [bpo-32690](https://bugs.python.org/issue32690) \[https://bugs.python.org/issue32690\] 中貢獻。) - [`distutils`](../library/distutils.xhtml#module-distutils "distutils: Support for building and installing Python modules into an existing Python installation.")`upload` 命令不會再試圖將行結束字符 CR 改為 CRLF。 這修復了 sdists 的一個以與 CR 等價的字節結束的數據損壞問題。 (由 Bo Bayles 在 [bpo-32304](https://bugs.python.org/issue32304) \[https://bugs.python.org/issue32304\] 中貢獻。) ## 已棄用的 Python 行為 在推導式和生成器表達式中的 yield 語句(包括 `yield` 和 `yield from` 子句)現在已棄用(最左端的 `for` 子句中的可迭代對象表達式除外)。 這確保了推導式總是立即返回適當類型的容器(而不是有可能返回 [generator iterator](../glossary.xhtml#term-generator-iterator) 對象),這樣生成器表達式不會試圖將它們的隱式輸出與任何來自顯式 yield 表達式的輸出交錯起來。 在 Python 3.7 中,這樣的表達式會在編譯時引發 [`DeprecationWarning`](../library/exceptions.xhtml#DeprecationWarning "DeprecationWarning"),在 Python 3.8 中則將引發 [`SyntaxError`](../library/exceptions.xhtml#SyntaxError "SyntaxError")。 (由 Serhiy Storchaka 在 [bpo-10544](https://bugs.python.org/issue10544) \[https://bugs.python.org/issue10544\] 中貢獻。) 從 [`object.__complex__()`](../reference/datamodel.xhtml#object.__complex__ "object.__complex__") 返回一個 [`complex`](../library/functions.xhtml#complex "complex") 的子類的行為已棄用并將在未來的 Python 版本中引發錯誤。 這使得 `__complex__()` 的行為與 [`object.__int__()`](../reference/datamodel.xhtml#object.__int__ "object.__int__") 和 [`object.__float__()`](../reference/datamodel.xhtml#object.__float__ "object.__float__") 保持一致。 (由 Serhiy Storchaka 在 [bpo-28894](https://bugs.python.org/issue28894) \[https://bugs.python.org/issue28894\] 中貢獻。) ## 已棄用的 Python 模塊、函數和方法 ### aifc `aifc.openfp()` 已棄用并將在 Python 3.9 中被移除。 請改用 [`aifc.open()`](../library/aifc.xhtml#aifc.open "aifc.open")。 (由 Brian Curtin 在 [bpo-31985](https://bugs.python.org/issue31985) \[https://bugs.python.org/issue31985\] 中貢獻。) ### asyncio 對 [`asyncio.Lock`](../library/asyncio-sync.xhtml#asyncio.Lock "asyncio.Lock") 和其他 asyncio 同步原語的 `await` 實例的直接支持已棄用。 想要獲取并釋放同步資源必須使用異步上下文管理器。 (由 Andrew Svetlov 在 [bpo-32253](https://bugs.python.org/issue32253) \[https://bugs.python.org/issue32253\] 中貢獻。) [`asyncio.Task.current_task()`](../library/asyncio-task.xhtml#asyncio.Task.current_task "asyncio.Task.current_task") 和 [`asyncio.Task.all_tasks()`](../library/asyncio-task.xhtml#asyncio.Task.all_tasks "asyncio.Task.all_tasks") 方法已棄用。 (由 Andrew Svetlov 在 [bpo-32250](https://bugs.python.org/issue32250) \[https://bugs.python.org/issue32250\] 中貢獻。) ### collections 在 Python 3.8 中,[`collections.abc`](../library/collections.abc.xhtml#module-collections.abc "collections.abc: Abstract base classes for containers") 內的抽象基類將不會再通過常規的 [`collections`](../library/collections.xhtml#module-collections "collections: Container datatypes") 模塊公開。 這有助于更清晰地區別具體類與抽象基類。 (由 Serhiy Storchaka 在 [bpo-25988](https://bugs.python.org/issue25988) \[https://bugs.python.org/issue25988\] 中貢獻。) ### dbm [`dbm.dumb`](../library/dbm.xhtml#module-dbm.dumb "dbm.dumb: Portable implementation of the simple DBM interface.") 現在支持讀取只讀文件,且當其未被更改時不會再寫入索引文件。 現在如果索引文件丟失并在 `'r'` 與 `'w'` 模式下被重新創建,則會發出已棄用警告(在未來的 Python 發布版中將改為錯誤)。 (由 Serhiy Storchaka 在 [bpo-28847](https://bugs.python.org/issue28847) \[https://bugs.python.org/issue28847\] 中貢獻。) ### enum 在 Python 3.8 中,嘗試在 `Enum` 類中檢查非 Enum 對象將引發 [`TypeError`](../library/exceptions.xhtml#TypeError "TypeError") (例如 `1 in Color`);類似地,嘗試在 `Flag` 成員中檢查非 Flag 對象也將引發 [`TypeError`](../library/exceptions.xhtml#TypeError "TypeError") (例如 `1 in Perm.RW`);目前,兩種操作均會返回 [`False`](../library/constants.xhtml#False "False")。 (由 Ethan Furman 在 [bpo-33217](https://bugs.python.org/issue33217) \[https://bugs.python.org/issue33217\] 中貢獻。) ### gettext 使用非整數值在 [`gettext`](../library/gettext.xhtml#module-gettext "gettext: Multilingual internationalization services.") 中選擇復數形式現在已棄用。 它從未正確地發揮作用。 (由 Serhiy Storchaka 在 [bpo-28692](https://bugs.python.org/issue28692) \[https://bugs.python.org/issue28692\] 中貢獻。) ### importlib 下列方法 [`MetaPathFinder.find_module()`](../library/importlib.xhtml#importlib.abc.MetaPathFinder.find_module "importlib.abc.MetaPathFinder.find_module") (被 [`MetaPathFinder.find_spec() 替代`](../library/importlib.xhtml#importlib.abc.MetaPathFinder.find_spec "importlib.abc.MetaPathFinder.find_spec")) 和 [`PathEntryFinder.find_loader()`](../library/importlib.xhtml#importlib.abc.PathEntryFinder.find_loader "importlib.abc.PathEntryFinder.find_loader") (被 [`PathEntryFinder.find_spec() 替代`](../library/importlib.xhtml#importlib.abc.PathEntryFinder.find_spec "importlib.abc.PathEntryFinder.find_spec")) 都在 Python 3.4 中已棄用,現在會引發 [`DeprecationWarning`](../library/exceptions.xhtml#DeprecationWarning "DeprecationWarning")。 (由 Matthias Bussonnier 在 [bpo-29576](https://bugs.python.org/issue29576) \[https://bugs.python.org/issue29576\] 中貢獻) [`importlib.abc.ResourceLoader`](../library/importlib.xhtml#importlib.abc.ResourceLoader "importlib.abc.ResourceLoader") ABC 已棄用,推薦改用 [`importlib.abc.ResourceReader`](../library/importlib.xhtml#importlib.abc.ResourceReader "importlib.abc.ResourceReader")。 ### locale [`locale.format()`](../library/locale.xhtml#locale.format "locale.format") 已棄用,請改用 [`locale.format_string()`](../library/locale.xhtml#locale.format_string "locale.format_string")。 (由 Garvit 在 [bpo-10379](https://bugs.python.org/issue10379) \[https://bugs.python.org/issue10379\] 中貢獻。) ### macpath [`macpath`](../library/macpath.xhtml#module-macpath "macpath: Mac OS 9 path manipulation functions.") 現在已棄用,將在 Python 3.8 中被移除。 (由 Chi Hsuan Yen 在 [bpo-9850](https://bugs.python.org/issue9850) \[https://bugs.python.org/issue9850\] 中貢獻。) ### threading [`dummy_threading`](../library/dummy_threading.xhtml#module-dummy_threading "dummy_threading: Drop-in replacement for the threading module.") 和 [`_dummy_thread`](../library/_dummy_thread.xhtml#module-_dummy_thread "_dummy_thread: Drop-in replacement for the _thread module.") 已棄用。 構建禁用線程的 Python 已不再可能。 請改用 [`threading`](../library/threading.xhtml#module-threading "threading: Thread-based parallelism.")。 (由 Antoine Pitrou 在 [bpo-31370](https://bugs.python.org/issue31370) \[https://bugs.python.org/issue31370\] 中貢獻。) ### socket [`socket.htons()`](../library/socket.xhtml#socket.htons "socket.htons") 和 [`socket.ntohs()`](../library/socket.xhtml#socket.ntohs "socket.ntohs") 中的靜默參數截斷已棄用。 在未來的 Python 版本中,如果傳入的參數長度大于 16 比特位,將會引發異常。 (由 Oren Milman 在 [bpo-28332](https://bugs.python.org/issue28332) \[https://bugs.python.org/issue28332\] 中貢獻。) ### ssl [`ssl.wrap_socket()`](../library/ssl.xhtml#ssl.wrap_socket "ssl.wrap_socket") 已棄用。 請改用 [`ssl.SSLContext.wrap_socket()`](../library/ssl.xhtml#ssl.SSLContext.wrap_socket "ssl.SSLContext.wrap_socket")。 (由 Christian Heimes 在 [bpo-28124](https://bugs.python.org/issue28124) \[https://bugs.python.org/issue28124\] 中貢獻。) ### sunau [`sunau.openfp()`](../library/sunau.xhtml#sunau.openfp "sunau.openfp") 已棄用并將在 Python 3.9 中被移除。 請改用 [`sunau.open()`](../library/sunau.xhtml#sunau.open "sunau.open")。 (由 Brian Curtin 在 [bpo-31985](https://bugs.python.org/issue31985) \[https://bugs.python.org/issue31985\] 中貢獻。) ### sys 已棄用 [`sys.set_coroutine_wrapper()`](../library/sys.xhtml#sys.set_coroutine_wrapper "sys.set_coroutine_wrapper") 和 [`sys.get_coroutine_wrapper()`](../library/sys.xhtml#sys.get_coroutine_wrapper "sys.get_coroutine_wrapper")。 未寫入文檔的 `sys.callstats()` 函數已棄用并將在未來的 Python 版本中被移除。 (由 Victor Stinner 在 [bpo-28799](https://bugs.python.org/issue28799) \[https://bugs.python.org/issue28799\] 中貢獻。) ### wave [`wave.openfp()`](../library/wave.xhtml#wave.openfp "wave.openfp") 已棄用并將在 Python 3.9 中被移除。 請改用 [`wave.open()`](../library/wave.xhtml#wave.open "wave.open")。 (由 Brian Curtin 在 [bpo-31985](https://bugs.python.org/issue31985) \[https://bugs.python.org/issue31985\] 中貢獻。) ## 已棄用的 C API 函數和類型 如果 `Py_LIMITED_API` 未設定或設定為范圍在 `0x03050400` 和 `0x03060000` (不含) 之間,或為 `0x03060100` 或更高的值,函數 [`PySlice_GetIndicesEx()`](../c-api/slice.xhtml#c.PySlice_GetIndicesEx "PySlice_GetIndicesEx") 已棄用并被一個宏所替代。 (由 Serhiy Storchaka 在 [bpo-27867](https://bugs.python.org/issue27867) \[https://bugs.python.org/issue27867\] 中貢獻。) [`PyOS_AfterFork()`](../c-api/sys.xhtml#c.PyOS_AfterFork "PyOS_AfterFork") 已棄用。 請改用 [`PyOS_BeforeFork()`](../c-api/sys.xhtml#c.PyOS_BeforeFork "PyOS_BeforeFork"), [`PyOS_AfterFork_Parent()`](../c-api/sys.xhtml#c.PyOS_AfterFork_Parent "PyOS_AfterFork_Parent") 或 [`PyOS_AfterFork_Child()`](../c-api/sys.xhtml#c.PyOS_AfterFork_Child "PyOS_AfterFork_Child")。 (由 Antoine Pitrou 在 [bpo-16500](https://bugs.python.org/issue16500) \[https://bugs.python.org/issue16500\] 中貢獻。) ## 平臺支持的移除 - 官方已不再支持 FreeBSD 9 及更舊的版本。 - 為了完整的 Unicode 支持,包括在擴展模塊之內,\*nix 平臺現在至少應當提供 `C.UTF-8` (完整區域), `C.utf8` (完整區域) 或 `UTF-8` (`LC_CTYPE` 專屬區域) 中的一個作為基于 `ASCII` 的傳統 `C` 區域的替代。 - OpenSSL 0.9.8 和 1.0.1 已不再受支持,這意味著在仍然使用這些版本的舊平臺上構建帶有 SSL/TLS 支持的 CPython 3.7 時,需要自定義構建選項以鏈接到更新的 OpenSSL 版本。 注意,此問題會影響到 Debian 8 (代號“jessie”) 和 Ubuntu 14.04 (代號“Trusty”) 等長期支持 Linux 發行版,因為它們默認仍然使用 OpenSSL 1.0.1。 Debian 9 (“stretch”) 和 Ubuntu 16.04 (“xenial”) 以及其他最新的長期支持 Linux 發行版 (例如 RHEL/CentOS 7.5, SLES 12-SP3) 都使用 OpenSSL 1.0.2 或更新的版本,因此繼續在默認的構建配置中受到支持。 CPython 自己的 [CI 配置文件](https://github.com/python/cpython/tree/3.7/.travis.yml) \[https://github.com/python/cpython/tree/3.7/.travis.yml\] 提供了一個使用 CPython 測試套件中的 SSL [兼容性測試架構](https://github.com/python/cpython/tree/3.7/Tools/ssl/multissltests.py) \[https://github.com/python/cpython/tree/3.7/Tools/ssl/multissltests.py\] 基于 OpenSSL 1.1.0 進行構建和鏈接的例子,而不是使用過時的系統所提供的 OpenSSL。 ## API 與特性的移除 下列特性與 API 已從 Python 3.7 中移除: - `os.stat_float_times()` 函數已被移除。 它在 Python 2.3 中被引入用于向下兼容 Python 2.2,并自 Python 3.1 起就已棄用。 - 在 [`re.sub()`](../library/re.xhtml#re.sub "re.sub") 的替換模塊中由 `'\'` 與一個 ASCII 字母構成的未知轉義在 Python 3.5 中已棄用,現在將會引發錯誤。 - 在 [`tarfile.TarFile.add()`](../library/tarfile.xhtml#tarfile.TarFile.add "tarfile.TarFile.add") 中移除了對 *exclude* 參數的支持。 它在 Python 2.7 和 3.2 中已棄用。 請改用 *filter* 參數。 - `ntpath` 模塊中的 `splitunc()` 函數在 Python 3.1 中已棄用,現在已被移除。 請改用 [`splitdrive()`](../library/os.path.xhtml#os.path.splitdrive "os.path.splitdrive") 函數。 - [`collections.namedtuple()`](../library/collections.xhtml#collections.namedtuple "collections.namedtuple") 不再支持 *verbose* 形參或 `_source` 屬性,該屬性會顯示為具名元組類所生成的源代碼。 這是加速類創建的設計優化的一部分。 (由 Jelle Zijlstra 在 [bpo-28638](https://bugs.python.org/issue28638) \[https://bugs.python.org/issue28638\] 中貢獻,進一步的改進由 INADA Naoki, Serhiy Storchaka 和 Raymond Hettinger 貢獻。) - 函數 [`bool()`](../library/functions.xhtml#bool "bool"), [`float()`](../library/functions.xhtml#float "float"), [`list()`](../library/stdtypes.xhtml#list "list") 和 [`tuple()`](../library/stdtypes.xhtml#tuple "tuple") 不再接受關鍵字參數。 [`int()`](../library/functions.xhtml#int "int") 的第一個參數現在只能作為位置參數傳入。 - 移除了之前在 Python 2.4 中已棄用的 [`plistlib`](../library/plistlib.xhtml#module-plistlib "plistlib: Generate and parse Mac OS X plist files.") 模塊的類 `Plist`, `Dict` 和 `_InternalDict`。 作為函數 [`readPlist()`](../library/plistlib.xhtml#plistlib.readPlist "plistlib.readPlist") 和 [`readPlistFromBytes()`](../library/plistlib.xhtml#plistlib.readPlistFromBytes "plistlib.readPlistFromBytes") 返回結果的 Dict 值現在為普通 dict。 你不能再使用屬性訪問來獲取這些字典的項。 - `asyncio.windows_utils.socketpair()` 函數已被移除。 請改用 [`socket.socketpair()`](../library/socket.xhtml#socket.socketpair "socket.socketpair") 函數,它自 Python 3.5 起就在所有平臺上可用。 `asyncio.windows_utils.socketpair` 在 Python 3.5 及更新版本上只是 `socket.socketpair` 的別名。 - [`asyncio`](../library/asyncio.xhtml#module-asyncio "asyncio: Asynchronous I/O.") 不再將 [`selectors`](../library/selectors.xhtml#module-selectors "selectors: High-level I/O multiplexing.") 和 `_overlapped` 模塊導出為 `asyncio.selectors` 和 `asyncio._overlapped`。 請將 `from asyncio import selectors` 替換為 `import selectors`。 - 現在已禁止直接實例化 [`ssl.SSLSocket`](../library/ssl.xhtml#ssl.SSLSocket "ssl.SSLSocket") 和 [`ssl.SSLObject`](../library/ssl.xhtml#ssl.SSLObject "ssl.SSLObject") 對象。 相應構造器從未寫入文檔、也從未作為公有構造器進行測試或設計。 用戶應當使用 [`ssl.wrap_socket()`](../library/ssl.xhtml#ssl.wrap_socket "ssl.wrap_socket") 或 [`ssl.SSLContext`](../library/ssl.xhtml#ssl.SSLContext "ssl.SSLContext")。 (由 Christian Heimes 在 [bpo-32951](https://bugs.python.org/issue32951) \[https://bugs.python.org/issue32951\] 中貢獻。) - 未被使用的 [`distutils`](../library/distutils.xhtml#module-distutils "distutils: Support for building and installing Python modules into an existing Python installation.")`install_misc` 命令已被移除。 (由 Eric N. Vander Weele 在 [bpo-29218](https://bugs.python.org/issue29218) \[https://bugs.python.org/issue29218\] 中貢獻。) ## 移除的模塊 `fpectl` 模塊已被移除。 它從未被默認啟用,從未在 x86-64 上正確發揮效果,并且它對 Python ABI 的改變會導致 C 擴展的意外損壞。 (由 Nathaniel J. Smith 在 [bpo-29137](https://bugs.python.org/issue29137) \[https://bugs.python.org/issue29137\] 中貢獻。) ## Windows 專屬的改變 Python 啟動器(py.exe)可以接受 32 位或 64 位標記而 **不必** 同時指定一個小版本。 因此 `py -3-32` 和 `py -3-64` 與 `py -3.7-32` 均為有效,并且現在還接受 -*m*-64 和 -*m.n*-64 來強制使用 64 位 python 命令,即使是本應使用 32 位的時候。 如果指定版本不可用則 py.exe 將報錯退出。 (由 Steve Barnes 在 [bpo-30291](https://bugs.python.org/issue30291) \[https://bugs.python.org/issue30291\] 中貢獻。) 啟動器可以運行 `py -0` 來列出已安裝的所有 python,*默認版本會以星號標出*。 運行 `py -0p` 將同時列出相應的路徑。 如果運行 py 時指定了無法匹配的版本,它將顯示 *簡短形式* 的可用版本列表。 (由 Steve Barnes 在 [bpo-30362](https://bugs.python.org/issue30362) \[https://bugs.python.org/issue30362\] 中貢獻。) ## 移植到 Python 3.7 本節列出了先前描述的更改以及可能需要更改代碼的其他錯誤修正. ### Python 行為的更改 - [`async`](../reference/compound_stmts.xhtml#async) 和 [`await`](../reference/expressions.xhtml#await) 現在是保留關鍵字。 使用了這些名稱作為標識符的代碼現在將引發 [`SyntaxError`](../library/exceptions.xhtml#SyntaxError "SyntaxError")。 (由 Jelle Zijlstra 在 [bpo-30406](https://bugs.python.org/issue30406) \[https://bugs.python.org/issue30406\] 中貢獻。) - [**PEP 479**](https://www.python.org/dev/peps/pep-0479) \[https://www.python.org/dev/peps/pep-0479\] 在 Python 3.7 中對所有代碼啟用,在協程和生成器中直接或間接引發的 [`StopIteration`](../library/exceptions.xhtml#StopIteration "StopIteration") 異常會被轉換為 [`RuntimeError`](../library/exceptions.xhtml#RuntimeError "RuntimeError") 異常。 (由 Yury Selivanov 在 [bpo-32670](https://bugs.python.org/issue32670) \[https://bugs.python.org/issue32670\] 中貢獻。) - [`object.__aiter__()`](../reference/datamodel.xhtml#object.__aiter__ "object.__aiter__") 方法不再能被聲明為異步的。 (由 Yury Selivanov 在 [bpo-31709](https://bugs.python.org/issue31709) \[https://bugs.python.org/issue31709\] 中貢獻。) - 由于一個疏忽,之前的 Python 版本會錯誤地接受以下語法: ``` f(1 for x in [1],) class C(1 for x in [1]): pass ``` 現在 Python 3.7 會正確地引發 [`SyntaxError`](../library/exceptions.xhtml#SyntaxError "SyntaxError"),因為生成器表達式總是必須直接包含于一對括號之內, 且前后都不能有逗號,僅在調用時可以忽略重復的括號。 (由 Serhiy Storchaka 在 [bpo-32012](https://bugs.python.org/issue32012) \[https://bugs.python.org/issue32012\] 和 [bpo-32023](https://bugs.python.org/issue32023) \[https://bugs.python.org/issue32023\] 中貢獻。) - 現在當使用 [`-m`](../using/cmdline.xhtml#cmdoption-m) 開關時,會將初始工作目錄添加到 [`sys.path`](../library/sys.xhtml#sys.path "sys.path"),而不再是一個空字符串(即在每次導入時動態地指明當前工作目錄)。 任何會檢測該空字符串,或是以其他方式依賴之前行為的的程序將需要進行相應的更新(例如改為還要檢測 `os.getcwd()` 或 `os.path.dirname(__main__.__file__)`,具體做法首先要取決于為何要對代碼執行空字符串檢測)。 ### 更改的Python API - `socketserver.ThreadingMixIn.server_close()` 現在會等待所有非守護線程完成。 將新增的 `socketserver.ThreadingMixIn.block_on_close` 類屬性設為 `False` 可獲得 3.7 之前版本的行為。 (由 Victor Stinner 在 [bpo-31233](https://bugs.python.org/issue31233) \[https://bugs.python.org/issue31233\] 和 [bpo-33540](https://bugs.python.org/issue33540) \[https://bugs.python.org/issue33540\] 中貢獻。) - `socketserver.ForkingMixIn.server_close()` 現在會等等所有子進程完成。 將新增的 `socketserver.ForkingMixIn.block_on_close` 類屬性設為 `False` 可獲得 3.7 之前版本的行為。 (由 Victor Stinner 在 [bpo-31151](https://bugs.python.org/issue31151) \[https://bugs.python.org/issue31151\] 和 [bpo-33540](https://bugs.python.org/issue33540) \[https://bugs.python.org/issue33540\] 中貢獻。) - 某些情況下 [`locale.localeconv()`](../library/locale.xhtml#locale.localeconv "locale.localeconv") 函數現在會臨時將 `LC_CTYPE` 區域設置為 `LC_NUMERIC` 的值。 (由 Victor Stinner 在 [bpo-31900](https://bugs.python.org/issue31900) \[https://bugs.python.org/issue31900\] 中貢獻。) - 如果 *path* 為字符串 [`pkgutil.walk_packages()`](../library/pkgutil.xhtml#pkgutil.walk_packages "pkgutil.walk_packages") 現在會引發 [`ValueError`](../library/exceptions.xhtml#ValueError "ValueError")。 之前則是返回一個空列表。 (由 Sanyam Khurana 在 [bpo-24744](https://bugs.python.org/issue24744) \[https://bugs.python.org/issue24744\] 中貢獻。) - [`string.Formatter.format()`](../library/string.xhtml#string.Formatter.format "string.Formatter.format") 的格式字符串參數現在為 [僅限位置](../glossary.xhtml#positional-only-parameter) 參數。 將其作為關鍵字參數傳入的方式自 Python 3.5 起已棄用。 (由 Serhiy Storchaka 在 [bpo-29193](https://bugs.python.org/issue29193) \[https://bugs.python.org/issue29193\] 中貢獻。) - 類 [`http.cookies.Morsel`](../library/http.cookies.xhtml#http.cookies.Morsel "http.cookies.Morsel") 的屬性 [`key`](../library/http.cookies.xhtml#http.cookies.Morsel.key "http.cookies.Morsel.key"), [`value`](../library/http.cookies.xhtml#http.cookies.Morsel.value "http.cookies.Morsel.value") 和 [`coded_value`](../library/http.cookies.xhtml#http.cookies.Morsel.coded_value "http.cookies.Morsel.coded_value") 現在均為只讀。 對其賦值的操作自 Python 3.5 起已棄用。 要設置它們的值請使用 [`set()`](../library/http.cookies.xhtml#http.cookies.Morsel.set "http.cookies.Morsel.set") 方法。 (由 Serhiy Storchaka 在 [bpo-29192](https://bugs.python.org/issue29192) \[https://bugs.python.org/issue29192\] 中貢獻。) - [`os.makedirs()`](../library/os.xhtml#os.makedirs "os.makedirs") 的 *mode* 參數不會再影響新建中間層級目錄的文件權限位。 要設置它們的文件權限位你可以在發起調用 `makedirs()` 之前設置 umask。 (由 Serhiy Storchaka 在 [bpo-19930](https://bugs.python.org/issue19930) \[https://bugs.python.org/issue19930\] 中貢獻。) - [`struct.Struct.format`](../library/struct.xhtml#struct.Struct.format "struct.Struct.format") 的類型現在是 [`str`](../library/stdtypes.xhtml#str "str") 而非 [`bytes`](../library/stdtypes.xhtml#bytes "bytes")。 (由 Victor Stinner 在 [bpo-21071](https://bugs.python.org/issue21071) \[https://bugs.python.org/issue21071\] 中貢獻。) - [`parse_multipart()`](../library/cgi.xhtml#cgi.parse_multipart "cgi.parse_multipart") 現在接受 *encoding* 和 *errors* 參數并返回與 `FieldStorage` 同樣的結果:對于非文件字段,與鍵相關聯的值是一個字符串列表,而非字節串。 (由 Pierre Quentel 在 [bpo-29979](https://bugs.python.org/issue29979) \[https://bugs.python.org/issue29979\] 中貢獻。) - 由于 [`socket`](../library/socket.xhtml#module-socket "socket: Low-level networking interface.") 中的內部更改,在由舊版 Python 中的 [`socket.share`](../library/socket.xhtml#socket.socket.share "socket.socket.share") 所創建的套接字上調用 [`socket.fromshare()`](../library/socket.xhtml#socket.fromshare "socket.fromshare") 已不受支持。 - [`BaseException`](../library/exceptions.xhtml#BaseException "BaseException") 的 `repr` 已更改為不包含末尾的逗號。 大多數異常都會受此更改影響。 (由 Serhiy Storchaka 在 [bpo-30399](https://bugs.python.org/issue30399) \[https://bugs.python.org/issue30399\] 中貢獻。) - [`datetime.timedelta`](../library/datetime.xhtml#datetime.timedelta "datetime.timedelta") 的 `repr` 已更改為在輸出中包含關鍵字參數。 (由 Utkarsh Upadhyay 在 [bpo-30302](https://bugs.python.org/issue30302) \[https://bugs.python.org/issue30302\] 中貢獻。) - 因為 [`shutil.rmtree()`](../library/shutil.xhtml#shutil.rmtree "shutil.rmtree") 現在是使用 [`os.scandir()`](../library/os.xhtml#os.scandir "os.scandir") 函數實現的,用戶指定的句柄 *onerror* 現在被調用時如果列目錄失敗會附帶第一個參數 `os.scandir` 而不是 `os.listdir`。 - 未來可能加入在正則表達式中對 [Unicode 技術標準 #18](https://unicode.org/reports/tr18/) \[https://unicode.org/reports/tr18/\] 中嵌套集合與集合操作的支持。 這會改變現有語法。 為了推動這項未來的改變,目前在有歧義的情況下會引發 [`FutureWarning`](../library/exceptions.xhtml#FutureWarning "FutureWarning")。 這包括以字面值 `'['` 開頭或包含字面值字符序列 `'--'`, `'&&'`, `'~~'` 以及 `'||'` 的集合。 要避免警告,請用反斜杠對其進行轉義。 (由 Serhiy Storchaka 在 [bpo-30349](https://bugs.python.org/issue30349) \[https://bugs.python.org/issue30349\] 中貢獻。) - 基于可以匹配空字符串的 [`正則表達式`](../library/re.xhtml#module-re "re: Regular expression operations.") 對字符串進行拆分的結果已被更改。 例如基于 `r'\s*'` 的拆分現在不僅會像原先那樣拆分空格符,而且會拆分所有非空格字符之前和字符串結尾處的空字符串。 通過將模式修改為 `r'\s+'` 可以恢復原先的行為。 自 Python 3.5 開始此類模式將會引發 [`FutureWarning`](../library/exceptions.xhtml#FutureWarning "FutureWarning")。 對于同時匹配空字符串和非空字符串的模式,在其他情況下搜索所有匹配的結果也可能會被更改。 例如在字符串 `'a\n\n'` 中,模式 `r'(?m)^\s*?$'` 將不僅會匹配位置 2 和 3 上的空字符串,還會匹配位置 2--3 上的字符串 `'\n'`。 想要只匹配空行,模式應當改寫為 `r'(?m)^[^\S\n]*$'`。 [`re.sub()`](../library/re.xhtml#re.sub "re.sub") 現在會替換與前一個的非空匹配相鄰的空匹配。 例如 `re.sub('x*', '-', 'abxd')` 現在會返回 `'-a-b--d-'` 而不是 `'-a-b-d-'` ('b' 和 'd' 之間的第一個減號是替換 'x',而第二個減號則是替換 'x' 和 'd' 之間的空字符串)。 (由 Serhiy Storchaka 在 [bpo-25054](https://bugs.python.org/issue25054) \[https://bugs.python.org/issue25054\] 和 [bpo-32308](https://bugs.python.org/issue32308) \[https://bugs.python.org/issue32308\] 中貢獻。) - [`re.escape()`](../library/re.xhtml#re.escape "re.escape") 更改為只轉義正則表達式特殊字符,而不轉義 ASCII 字母、數字和 `'_'` 以外的所有字符。 (由 Serhiy Storchaka 在 [bpo-29995](https://bugs.python.org/issue29995) \[https://bugs.python.org/issue29995\] 中貢獻。) - [`tracemalloc.Traceback`](../library/tracemalloc.xhtml#tracemalloc.Traceback "tracemalloc.Traceback") 幀現在是按從最舊到最新排序,以便與 [`traceback`](../library/traceback.xhtml#module-traceback "traceback: Print or retrieve a stack traceback.") 更為一致。 (由 Jesse Bakker 在 [bpo-32121](https://bugs.python.org/issue32121) \[https://bugs.python.org/issue32121\] 中貢獻。) - 在支持 [`socket.SOCK_NONBLOCK`](../library/socket.xhtml#socket.SOCK_NONBLOCK "socket.SOCK_NONBLOCK") 或 [`socket.SOCK_CLOEXEC`](../library/socket.xhtml#socket.SOCK_CLOEXEC "socket.SOCK_CLOEXEC") 標志位的操作系統上,[`socket.type`](../library/socket.xhtml#socket.socket.type "socket.socket.type") 不再應用它們。 因此,像 `if sock.type == socket.SOCK_STREAM` 之類的檢測會在所有平臺上按預期的方式工作。 (由 Yury Selivanov 在 [bpo-32331](https://bugs.python.org/issue32331) \[https://bugs.python.org/issue32331\] 中貢獻。) - 在 Windows 上當重定向標準句柄時,[`subprocess.Popen`](../library/subprocess.xhtml#subprocess.Popen "subprocess.Popen") 的 *close\_fds* 參數的默認值從 [`False`](../library/constants.xhtml#False "False") 更改為 [`True`](../library/constants.xhtml#True "True")。 如果你以前依賴于在使用帶有標準 io 重定向的 [`subprocess.Popen`](../library/subprocess.xhtml#subprocess.Popen "subprocess.Popen") 時所繼承的句柄,則必須傳入 `close_fds=False` 以保留原先的行為,或是使用 [`STARTUPINFO.lpAttributeList`](../library/subprocess.xhtml#subprocess.STARTUPINFO.lpAttributeList "subprocess.STARTUPINFO.lpAttributeList")。 - [`importlib.machinery.PathFinder.invalidate_caches()`](../library/importlib.xhtml#importlib.machinery.PathFinder.invalidate_caches "importlib.machinery.PathFinder.invalidate_caches") -- 此方法隱式地影響 [`importlib.invalidate_caches()`](../library/importlib.xhtml#importlib.invalidate_caches "importlib.invalidate_caches") -- 現在會刪除 [`sys.path_importer_cache`](../library/sys.xhtml#sys.path_importer_cache "sys.path_importer_cache") 中被設為 `None` 的條目。 (由 Brett Cannon 在 [bpo-33169](https://bugs.python.org/issue33169) \[https://bugs.python.org/issue33169\] 中貢獻。) - 在 [`asyncio`](../library/asyncio.xhtml#module-asyncio "asyncio: Asynchronous I/O.") 中,[`loop.sock_recv()`](../library/asyncio-eventloop.xhtml#asyncio.loop.sock_recv "asyncio.loop.sock_recv"), [`loop.sock_sendall()`](../library/asyncio-eventloop.xhtml#asyncio.loop.sock_sendall "asyncio.loop.sock_sendall"), [`loop.sock_accept()`](../library/asyncio-eventloop.xhtml#asyncio.loop.sock_accept "asyncio.loop.sock_accept"), [`loop.getaddrinfo()`](../library/asyncio-eventloop.xhtml#asyncio.loop.getaddrinfo "asyncio.loop.getaddrinfo"), [`loop.getnameinfo()`](../library/asyncio-eventloop.xhtml#asyncio.loop.getnameinfo "asyncio.loop.getnameinfo") 已被更改為正確的協程方法以與培訓五日文檔相匹配。 之前,這些方法會返回 [`asyncio.Future`](../library/asyncio-future.xhtml#asyncio.Future "asyncio.Future") 實例。 (由 Yury Selivanov 在 [bpo-32327](https://bugs.python.org/issue32327) \[https://bugs.python.org/issue32327\] 中貢獻。) - [`asyncio.Server.sockets`](../library/asyncio-eventloop.xhtml#asyncio.Server.sockets "asyncio.Server.sockets") 現在會返回服務器套接字列表的副本,而不是直接地返回它。 (由 Yury Selivanov 在 [bpo-32662](https://bugs.python.org/issue32662) \[https://bugs.python.org/issue32662\] 中貢獻。) - [`Struct.format`](../library/struct.xhtml#struct.Struct.format "struct.Struct.format") 現在是一個 [`str`](../library/stdtypes.xhtml#str "str") 實例而非 [`bytes`](../library/stdtypes.xhtml#bytes "bytes") 實例。 (由 Victor Stinner 在 [bpo-21071](https://bugs.python.org/issue21071) \[https://bugs.python.org/issue21071\] 中貢獻。) - [`ast.literal_eval()`](../library/ast.xhtml#ast.literal_eval "ast.literal_eval") 現在更為嚴格。 任意地加減數字已不再被允許。 (由 Serhiy Storchaka 在 [bpo-31778](https://bugs.python.org/issue31778) \[https://bugs.python.org/issue31778\] 中貢獻。) - 當一個日期超出 `0001-01-01` 到 `9999-12-31` 范圍時 [`Calendar.itermonthdates`](../library/calendar.xhtml#calendar.Calendar.itermonthdates "calendar.Calendar.itermonthdates") 現在將始終如一地引發異常,以便支持不能容忍此類異常的應用程序,可以使用新增的 [`Calendar.itermonthdays3`](../library/calendar.xhtml#calendar.Calendar.itermonthdays3 "calendar.Calendar.itermonthdays3") 和 [`Calendar.itermonthdays4`](../library/calendar.xhtml#calendar.Calendar.itermonthdays4 "calendar.Calendar.itermonthdays4")。 這些新方法返回元組,并且其不受 [`datetime.date`](../library/datetime.xhtml#datetime.date "datetime.date") 所支持的范圍限制。 (由 Alexander Belopolsky 在 [bpo-28292](https://bugs.python.org/issue28292) \[https://bugs.python.org/issue28292\] 中貢獻。) - [`collections.ChainMap`](../library/collections.xhtml#collections.ChainMap "collections.ChainMap") 現在會保留底層映射的順序。 (由 Raymond Hettinger 在 [bpo-32792](https://bugs.python.org/issue32792) \[https://bugs.python.org/issue32792\] 中貢獻。) - 如果在解釋器關閉期間被調用,[`concurrent.futures.ThreadPoolExecutor`](../library/concurrent.futures.xhtml#concurrent.futures.ThreadPoolExecutor "concurrent.futures.ThreadPoolExecutor") 和 [`concurrent.futures.ProcessPoolExecutor`](../library/concurrent.futures.xhtml#concurrent.futures.ProcessPoolExecutor "concurrent.futures.ProcessPoolExecutor") 的 `submit()` 方法現在會引發 [`RuntimeError`](../library/exceptions.xhtml#RuntimeError "RuntimeError")。 (由 Mark Nemec 在 [bpo-33097](https://bugs.python.org/issue33097) \[https://bugs.python.org/issue33097\] 中貢獻。) - [`configparser.ConfigParser`](../library/configparser.xhtml#configparser.ConfigParser "configparser.ConfigParser") 構造器現在使用 `read_dict()` 來處理默認值,以使其行為與解析器的其余部分保持致。 在默認字典中的非字符串鍵和值現在會被隱式地轉換為字符串。 (由 James Tocknell 在 [bpo-23835](https://bugs.python.org/issue23835) \[https://bugs.python.org/issue23835\] 中貢獻。) - 一些未寫入文檔的內部導入已被移除。 一個例子是 `os.errno` 已不再可用;應改為直接使用 `import errno`。 請注意此類未寫入文檔的內部導入可能未經通知地隨時被移除,甚至是在微版本號發行版中移除。 ### C API 中的改變 函數 [`PySlice_GetIndicesEx()`](../c-api/slice.xhtml#c.PySlice_GetIndicesEx "PySlice_GetIndicesEx") 被認為對于大小可變的序列來說并不安全。 如果切片索引不是 [`int`](../library/functions.xhtml#int "int") 的實例,而是實現了 `__index__()` 方法的對象,則序列可以在其長度被傳給 `PySlice_GetIndicesEx()` 之后調整大小。 這可能導致返回超出序列長度的索引號。 為了避免可能的問題,請使用新增的函數 [`PySlice_Unpack()`](../c-api/slice.xhtml#c.PySlice_Unpack "PySlice_Unpack") 和 [`PySlice_AdjustIndices()`](../c-api/slice.xhtml#c.PySlice_AdjustIndices "PySlice_AdjustIndices")。 (由 Serhiy Storchaka 在 [bpo-27867](https://bugs.python.org/issue27867) \[https://bugs.python.org/issue27867\] 中貢獻。) ### CPython 字節碼的改變 新增了兩個操作碼: [`LOAD_METHOD`](../library/dis.xhtml#opcode-LOAD_METHOD) 和 [`CALL_METHOD`](../library/dis.xhtml#opcode-CALL_METHOD)。 (由 Yury Selivanov 和 INADA Naoki 在 [bpo-26110](https://bugs.python.org/issue26110) \[https://bugs.python.org/issue26110\] 中貢獻。) `STORE_ANNOTATION` 操作碼已被移除。 (由 Mark Shannon 在 [bpo-32550](https://bugs.python.org/issue32550) \[https://bugs.python.org/issue32550\] 中貢獻。) ### Windows 專屬的改變 用于重載 [`sys.path`](../library/sys.xhtml#sys.path "sys.path") 的文件現在命名為 `<python-executable>._pth` 而不是 `'sys.path'`。 請參閱 [查找模塊](../using/windows.xhtml#finding-modules) 了解更多信息。 (由 Steve Dower 在 [bpo-28137](https://bugs.python.org/issue28137) \[https://bugs.python.org/issue28137\] 中貢獻。) ### 其他 CPython 實現的改變 為了準備在未來對公開的 CPython 運行時初始化 API 進行潛在更改(請參閱 [**PEP 432**](https://www.python.org/dev/peps/pep-0432) \[https://www.python.org/dev/peps/pep-0432\] 獲取最初但略為過時的文稿),CPython 內部的啟動和配置管理邏輯已經過大幅重構。 雖然這些更新旨在對嵌入式應用程序和常規的 CPython CLI 用戶都完全透明,但它們在這里被提及是因為重構會改變解釋器啟動期間許多操作的內部順序,因此可能提示出原先隱藏的缺陷,這可能存在于嵌入式應用程序中,或是在 CPython 自身內部。 (最初由 Nick Coghlan 和 Eric Snow 作為 [bpo-22257](https://bugs.python.org/issue22257) \[https://bugs.python.org/issue22257\] 的一部分貢獻,并由 Nick, Eric 和 Victor Stinner 在一系列其他問題報告中進一步更新)。 已知會受到影響的一些細節: - [`PySys_AddWarnOptionUnicode()`](../c-api/sys.xhtml#c.PySys_AddWarnOptionUnicode "PySys_AddWarnOptionUnicode") 目前對嵌入式應用程序不可用,因為在調用 Py\_Initialize 之前需要創建 Unicode 對象。 請改用 [`PySys_AddWarnOption()`](../c-api/sys.xhtml#c.PySys_AddWarnOption "PySys_AddWarnOption")。 - 嵌入式應用程序通過 [`PySys_AddWarnOption()`](../c-api/sys.xhtml#c.PySys_AddWarnOption "PySys_AddWarnOption") 所添加的警告過濾器現在應該以更高的一致性優先于由解釋器所設置的默認過濾器 由于默認警告過濾器的配置方式發生了變化,將 [`Py_BytesWarningFlag`](../c-api/init.xhtml#c.Py_BytesWarningFlag "Py_BytesWarningFlag") 設置為大于一的值不再足以在發出 [`BytesWarning`](../library/exceptions.xhtml#BytesWarning "BytesWarning") 消息的同時將其轉換為異常。 而是改為必須設置旗標(以便首先發出警告),以及添加顯式的 `error::BytesWarning` 警告過濾器來將其轉換為異常。 由于編譯器處理文檔字符串的方式發生了變化,一個僅由文檔字符串構成的函數體中隱式的 `return None` 現在被標記為在與文檔字符串相同的行,而不是在函數的標題行。 當前異常狀態已從幀對象移到協程對象。 這會簡化解釋器并修正由于在進入或退出生成器時具有交換異常狀態而導致的一些模糊錯誤。 (由 Mark Shannon 在 [bpo-25612](https://bugs.python.org/issue25612) \[https://bugs.python.org/issue25612\] 中貢獻。) ## Python 3.7.1 中的重要變化 從 3.7.1 開始,[`Py_Initialize()`](../c-api/init.xhtml#c.Py_Initialize "Py_Initialize") 現在始終會讀取并遵循與 [`Py_Main()`](../c-api/veryhigh.xhtml#c.Py_Main "Py_Main") 相同的環境設置(在更早的 Python 版本中,它會遵循一個錯誤定義的環境變量子集,而在 Python 3.7.0 中則會由于 [bpo-34247](https://bugs.python.org/issue34247) \[https://bugs.python.org/issue34247\] 而完全不讀取它們)。 如果不想要此行為,請在調用 [`Py_Initialize()`](../c-api/init.xhtml#c.Py_Initialize "Py_Initialize") 之前將 [`Py_IgnoreEnvironmentFlag`](../c-api/init.xhtml#c.Py_IgnoreEnvironmentFlag "Py_IgnoreEnvironmentFlag") 設為 1。 在 3.7.1 中,上下文變量的 C API 已 [獲得更新](../c-api/contextvars.xhtml#contextvarsobjects-pointertype-change) 以使用 [`PyObject`](../c-api/structures.xhtml#c.PyObject "PyObject") 指針。 另請參閱 [bpo-34762](https://bugs.python.org/issue34762) \[https://bugs.python.org/issue34762\]。 在默認情況下 [`xml.dom.minidom`](../library/xml.dom.minidom.xhtml#module-xml.dom.minidom "xml.dom.minidom: Minimal Document Object Model (DOM) implementation.") 和 [`xml.sax`](../library/xml.sax.xhtml#module-xml.sax "xml.sax: Package containing SAX2 base classes and convenience functions.") 模塊將不再處理外部實體。 另請參閱 [bpo-17239](https://bugs.python.org/issue17239) \[https://bugs.python.org/issue17239\]。 在 3.7.1 中,當提供不帶末尾新行的輸入時 [`tokenize`](../library/tokenize.xhtml#module-tokenize "tokenize: Lexical scanner for Python source code.") 模塊現在會隱式地添加 `NEWLINE` 形符。 此行為現在已與 C 詞法分析器的內部行為相匹配。 (由 Ammar Askar 在 [bpo-33899](https://bugs.python.org/issue33899) \[https://bugs.python.org/issue33899\] 中貢獻。) ## Python 3.7.2 中的重要變化 在 3.7.2 中,Windows 下的 [`venv`](../library/venv.xhtml#module-venv "venv: Creation of virtual environments.") 不再復制原來的二進制文件,而是改為創建名為 `python.exe` 和 `pythonw.exe` 的重定向腳本。 這解決了一個長期存在的問題,即所有虛擬環境在每次 Python 升級后都必須進行升級或是重新創建。 然而,要注意此發布版仍然要求重新創建虛擬環境以獲得新的腳本。 ### 導航 - [索引](../genindex.xhtml "總目錄") - [模塊](../py-modindex.xhtml "Python 模塊索引") | - [下一頁](3.6.xhtml "Python 3.6 有什么新變化A") | - [上一頁](index.xhtml "Python 有什么新變化?") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) ? - zh\_CN 3.7.3 [文檔](../index.xhtml) ? - [Python 有什么新變化?](index.xhtml) ? - $('.inline-search').show(0); | ? [版權所有](../copyright.xhtml) 2001-2019, Python Software Foundation. Python 軟件基金會是一個非盈利組織。 [請捐助。](https://www.python.org/psf/donations/) 最后更新于 5月 21, 2019. [發現了問題](../bugs.xhtml)? 使用[Sphinx](http://sphinx.pocoo.org/)1.8.4 創建。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看