GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • 目錄
  • __future__ --- Future 陳述式定義
  • traceback --- 列印或取得堆疊回溯 (stack traceback)
  • gc --- 垃圾回收器介面 (Garbage Collector interface)
  • 回報錯誤
  • 改進此頁面
  • 顯示原始碼
  • 顯示翻譯原始碼
  • 索引
  • 模組
  • Python
  • 3.15.2 documentation
  • Python 標準函式庫 (Standard Library)
  • Python Runtime 服務

This view

  • Original page
  • New address

GeezerVison is showing python.github.io — __future__ --- Future 陳述式定義 — Python 3.15.2 說明文件

__future__ --- Future 陳述式定義¶

原始碼:Lib/__future__.py


from __future__ import feature 形式的引入被稱為 future 陳述式。這些是 Python 編譯器的特殊情況,允許在該功能成為標準版本之前在包含 future 陳述式的模組中使用新的 Python 功能。

雖然這些 future 陳述式被 Python 編譯器賦予了額外的特殊意義,但它們仍然像任何其他 import 陳述式一樣執行,且 __future__ 由引入系統以和任何其他 Python 模組相同的方式處理。這個設計有三個目的:

  • 為了避免混淆分析引入陳述式並預期要找到它們正在引入之模組的現有工具。

  • 記錄何時出現不相容的變更,以及何時開始強制執行這些變更。這是一種可執行文件的形式,可以透過引入 __future__ 並檢查其內容以程式化的方式進行檢查。

  • 確保 future 陳述式在 Python 2.1 之前的版本中運行至少會產生 runtime 例外(__future__ 的引入將會失敗,因為 2.1 之前沒有該名稱的模組)。

模組內容¶

不會從 __future__ 中刪除任何功能描述。自從在 Python 2.1 中引入以來,以下功能已透過這種機制引入到該語言中:

功能

可選的版本

強制性的版本

影響

__future__.nested_scopes¶

2.1.0b1

2.2

PEP 227: 靜態巢狀作用域 (Statically Nested Scopes)

__future__.generators¶

2.2.0a1

2.3

PEP 255: 簡單產生器 (Simple Generators)

__future__.division¶

2.2.0a2

3.0

PEP 238: 更改除法運算子 (Changing the Division Operator)

__future__.absolute_import¶

2.5.0a1

3.0

PEP 328: 引入:多列與絕對/相對 (Imports: Multi-Line and Absolute/Relative)

__future__.with_statement¶

2.5.0a1

2.6

PEP 343: "with" 陳述式 (The "with" Statement)

__future__.print_function¶

2.6.0a2

3.0

PEP 3105: 使 print 成為一個函式 (Make print a function)

__future__.unicode_literals¶

2.6.0a2

3.0

PEP 3112: Python 3000 中的位元組字面值 (Bytes literals in Python 3000)

__future__.generator_stop¶

3.5.0b1

3.7

PEP 479: 產生器內部的 StopIteration 處理 (StopIteration handling inside generators)

__future__.annotations¶

3.7.0b1

從未 [1]

PEP 563: 註釋的延後求值 (Postponed evaluation of annotations)、PEP 649: 使用描述器延遲求值註釋 (Deferred evaluation of annotations using descriptors)

class __future__._Feature¶

__future__.py 中的每個陳述式的形式如下:

FeatureName = _Feature(OptionalRelease, MandatoryRelease,
                       CompilerFlag)

通常,OptionalRelease 會小於 MandatoryRelease,且兩者都是與 sys.version_info 形式相同的 5 元組 (5-tuple):

(PY_MAJOR_VERSION, # 2.1.0a3 裡面的 2;為一整數
 PY_MINOR_VERSION, # 1;為一整數
 PY_MICRO_VERSION, # 0;為一整數
 PY_RELEASE_LEVEL, # "alpha"、"beta"、"candidate" 或 "final"; ;為一字串
 PY_RELEASE_SERIAL # 3;為一整數
)
_Feature.getOptionalRelease()¶

OptionalRelease 記錄該功能首次發布時的 Python 版本。

_Feature.getMandatoryRelease()¶

如果 MandatoryRelease 尚未發布,MandatoryRelease 會預測該功能將成為該語言一部分的版本。

否則 MandatoryRelease 會記錄該功能是何時成為語言的一部分;在該版本或之後的版本中,模組不再需要 future 聲明來使用相關功能,但可以繼續使用此種引入方式。

MandatoryRelease 也可能是 None,這意味著計劃中的功能被丟棄或者仍未決定。

_Feature.compiler_flag¶

CompilerFlag 是(位元欄位 (bitfield))旗標,應在第四個引數中傳遞給內建函式 compile() 以在動態編譯的程式碼中啟用該功能。此旗標儲存在 _Feature 實例上的 _Feature.compiler_flag 屬性中。

[1]

from __future__ import annotations 原本預定在 Python 3.10 中成為強制性功能,但該變更被延後並最終取消。此功能最終將被棄用並移除。請參閱 PEP 649 和 PEP 749。

也參考

Future 陳述式

編譯器如何處理 future 引入。

PEP 236 - 回到 __future__

__future__ 機制的原始提案。

© 版權所有 2001 Python Software Foundation.
此頁面採用 Python 軟體基金會授權條款第 2 版。
文件中的範例、應用技巧與其他程式碼額外採用了 Zero Clause BSD 授權條款。
更多訊息請見歷史與授權條款。

Python 軟體基金會是一家非營利法人。 敬請捐贈。

最後更新於 11月 11, 2026 (06:44 UTC)。 發現 bug?
以 Sphinx8.2.3建立。
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.