__future__ —将来的语句定义

源代码: Lib/future.py


future是一个真实的模块,具有三个目的:

__future__.py中的每个语句的格式为:

FeatureName = _Feature(OptionalRelease, MandatoryRelease,
                       CompilerFlag)

通常,其中* OptionalRelease 小于 MandatoryRelease *,并且都是 5Tuples,其形式与sys.version_info相同:

(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
)

如果尚未发生* MandatoryRelease ,则 MandatoryRelease *会预测该Function将成为语言一部分的版本。

其他强制发布记录Function何时成为语言的一部分;在那时或之后的版本中,模块不再需要将来的语句来使用所涉及的Function,而是可以 continue 使用此类导入。

_Feature类的实例具有两个相应的方法getOptionalRelease()getMandatoryRelease()

future不会删除任何Function描述。自从 Python 2.1 引入以来,以下Function已使用此机制进入了语言:

feature optional in mandatory in effect
nested_scopes 2.1.0b1 2.2 PEP 227静态嵌套范围
generators 2.2.0a1 2.3 PEP 255简单的生成器
division 2.2.0a2 3.0 PEP 238更改部门操作员
absolute_import 2.5.0a1 3.0 PEP 328导入:多行和绝对/相对
with_statement 2.5.0a1 2.6 PEP 343“ with”语句
print_function 2.6.0a2 3.0 PEP 3105打印Function
unicode_literals 2.6.0a2 3.0 PEP 3112:* Python 3000 中的字节字面量*
generator_stop 3.5.0b1 3.7 PEP 479发生器内部的 StopIteration 处理
annotations 3.7.0b1 4.0 PEP 563Comments 的评估延后

See also

首页