__future__
--- future 文の定義¶
ソースコード: Lib/__future__.py
Imports of the form from __future__ import feature
are called
future statements. These are special-cased by the Python compiler
to allow the use of new Python features in modules containing the future statement
before the release in which the feature becomes standard.
While these future statements are given additional special meaning by the
Python compiler, they are still executed like any other import statement and
the __future__
exists and is handled by the import system the same way
any other Python module would be. This design serves three purposes:
import 文を解析する既存のツールを混乱させることを避け、インポートしようとしているモジュールを見つけられるようにするため。
互換性のない変化がいつ言語に導入され、いつ言語の一部になる --- あるいは、なった --- のかを文書化するため。これは実行できる形式で書かれたドキュメントなので、
__future__
をインポートしてその中身を調べることでプログラムから調査することができます。To ensure that future statements run under releases prior to Python 2.1 at least yield runtime exceptions (the import of
__future__
will fail, because there was no module of that name prior to 2.1).
モジュールコンテンツ¶
機能の記述が __future__
から削除されたことはまだありません。 Python 2.1 で future 文が導入されて以来、この仕組みを使って以下の機能が言語に導入されてきました:
feature |
optional in |
mandatory in |
effect |
---|---|---|---|
nested_scopes |
2.1.0b1 |
2.2 |
PEP 227: Statically Nested Scopes |
generators |
2.2.0a1 |
2.3 |
PEP 255: Simple Generators |
division |
2.2.0a2 |
3.0 |
PEP 238: Changing the Division Operator |
absolute_import |
2.5.0a1 |
3.0 |
PEP 328: Imports: Multi-Line and Absolute/Relative |
with_statement |
2.5.0a1 |
2.6 |
PEP 343: The "with" Statement |
print_function |
2.6.0a2 |
3.0 |
PEP 3105: Make print a function |
unicode_literals |
2.6.0a2 |
3.0 |
PEP 3112: Bytes literals in Python 3000 |
generator_stop |
3.5.0b1 |
3.7 |
PEP 479: StopIteration handling inside generators |
annotations |
3.7.0b1 |
TBD [1] |
PEP 563: Postponed evaluation of annotations |
- class __future__._Feature¶
__future__.py
のそれぞれの文は次のような形式をしています:FeatureName = _Feature(OptionalRelease, MandatoryRelease, CompilerFlag)
ここで、普通は、 OptionalRelease は MandatoryRelease より小さく、2つとも
sys.version_info
と同じフォーマットの5つのタプルからなります:(PY_MAJOR_VERSION, # the 2 in 2.1.0a3; an int PY_MINOR_VERSION, # the 1; an int PY_MICRO_VERSION, # the 0; an int PY_RELEASE_LEVEL, # "alpha", "beta", "candidate" or "final"; string PY_RELEASE_SERIAL # the 3; an int )
- _Feature.getOptionalRelease()¶
OptionalRelease はその機能が導入された最初のリリースを記録します。
- _Feature.getMandatoryRelease()¶
まだ時期が来ていない MandatoryRelease の場合、MandatoryRelease はその機能が言語の一部となるリリースを記します。
その他の場合、MandatoryRelease はその機能がいつ言語の一部になったのかを記録します。そのリリースから、あるいはそれ以降のリリースでは、この機能を使う際に future 文は必要ではありませんが、future 文を使い続けても構いません。
MandatoryRelease may also be
None
, meaning that a planned feature got dropped or that it is not yet decided.
- _Feature.compiler_flag¶
CompilerFlag is the (bitfield) flag that should be passed in the fourth argument to the built-in function
compile()
to enable the feature in dynamically compiled code. This flag is stored in the_Feature.compiler_flag
attribute on_Feature
instances.
参考
- future 文 (future statement)
コンパイラがどのように future インポートを扱うか。
- PEP 236 - Back to the __future__
__future__ 機構の原案