__future__
--- Future statement definitions¶
Kod źródłowy: 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:
Aby uniknąć pomyłek z istniejącymi narzędziami, które analizują instrukcje importowania i oczekują znalezienia modułów, które importują.
Щоб задокументувати, коли були внесені несумісні зміни, і коли вони будуть --- або були --- зроблені обов'язковими. Це форма виконуваної документації, яку можна перевірити програмним шляхом, імпортувавши
__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).
Modul-Modul¶
Жоден опис функції ніколи не буде видалено з __future__
. З моменту появи в Python 2.1 наступні функції знайшли шлях до мови за допомогою цього механізму:
fitur |
opcjonalne w |
obowiązkowe w |
efek |
---|---|---|---|
|
2.1.0b1 |
2.2 |
PEP 227: Statically Nested Scopes |
|
2.2.0a1 |
2.3 |
PEP 255: Simple Generators |
|
2.2.0a2 |
3.0 |
PEP 238: Changing the Division Operator |
|
2.5.0a1 |
3.0 |
PEP 328: Imports: Multi-Line and Absolute/Relative |
|
2.5.0a1 |
2.6 |
PEP 343: The “with” Statement |
|
2.6.0a2 |
3.0 |
PEP 3105: Make print a function |
|
2.6.0a2 |
3.0 |
PEP 3112: Bytes literals in Python 3000 |
|
3.5.0b1 |
3.7 |
PEP 479: StopIteration handling inside generators |
|
3.7.0b1 |
Never [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, і обидва є 5-кортежами тієї самої форми, що й
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 )
- _Feature.getOptionalRelease()¶
OptionalRelease записує перший випуск, у якому цю функцію було прийнято.
- _Feature.getMandatoryRelease()¶
У випадку MandatoryRelease, який ще не відбувся, MandatoryRelease передбачає випуск, у якому ця функція стане частиною мови.
Інакше MandatoryRelease записує, коли функція стала частиною мови; у випусках на цьому або після цього модулям більше не потрібен майбутній оператор для використання відповідної функції, але вони можуть продовжувати використовувати такий імпорт.
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.
Lihat juga
- Future statements
Jak kompilator traktuje przyszłe importy.
- PEP 236 - Back to the __future__
The original proposal for the __future__ mechanism.