How does the __future__ module work in Python 2.7?
本问题已经有最佳答案,请猛点这里访问。
在使用
1 2 | from __future__ import print_function print("Hello world!") |
python 2.1中引入了
因此,该模块提供了在早期版本中使用未来版本的不兼容功能的可能性。因此,您可以利用这些功能即将到来的优势。
如文档中所述,该模块有三个主要原因:
__future__ is a real module, and serves three purposes:
- To avoid confusing existing tools that analyze import statements and
expect to find the modules they’re importing.- To ensure that future
statements run under releases prior to 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).- To document when incompatible
changes were introduced, and when they will be — or were — made
mandatory. This is a form of executable documentation, and can be
inspected programmatically via importing__future__ and examining its
contents.