__future__ — Definições de instruções future

Código-fonte: Lib/__future__.py


Instruções do tipo from __future__ import feature são chamadas de instruções future. Esses são casos especiais do compilador do Python para permitir o uso de novos recursos do Python em módulos que contêm a instrução future antes da versão em que o recurso se torna padrão.

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:

  • Para evitar confundir as ferramentas existentes que analisam as instruções de importação e esperam encontrar os módulos sendo importados.

  • 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.

  • 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).

Conteúdo do módulo

No feature description will ever be deleted from __future__. Since its introduction in Python 2.1 the following features have found their way into the language using this mechanism:

característica

opcional em

obrigatório em

efeito

__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: The “with” Statement

__future__.print_function

2.6.0a2

3.0

PEP 3105: Make print a function

__future__.unicode_literals

2.6.0a2

3.0

PEP 3112: Bytes literals in Python 3000

__future__.generator_stop

3.5.0b1

3.7

PEP 479: StopIteration handling inside generators

__future__.annotations

3.7.0b1

Nunca [1]

PEP 563: Avaliação adiada de anotações, PEP 649: Avaliação diferida de anotações usando descritores

class __future__._Feature

Cada instrução em __future__.py é da forma:

FeatureName = _Feature(OptionalRelease, MandatoryRelease,
                       CompilerFlag)

Onde, normalmente, OptionalRelease é inferior a MandatoryRelease, e ambos são tuplas de 5 entradas da mesma forma que sys.version_info:

(PY_MAJOR_VERSION, # o 2 em 2.1.0a3; um int
 PY_MINOR_VERSION, # o 1; um int
 PY_MICRO_VERSION, # o 0; um int
 PY_RELEASE_LEVEL, # "alpha", "beta", "candidate" ou "final"; uma string
 PY_RELEASE_SERIAL # o 3; um int
)
_Feature.getOptionalRelease()

OptionalRelease registra o primeiro lançamento no qual o recurso foi aceito.

_Feature.getMandatoryRelease()

No caso de um MandatoryRelease que ainda não ocorreu, MandatoryRelease prevê o lançamento em que o recurso se tornará parte da linguagem.

Senão MandatoryRelease registra quando o recurso se tornou parte do idioma; Em versões em ou depois disso, os módulos não precisam mais de uma instrução future para usar o recurso em questão, mas podem continuar a usar essas importações.

MandatoryRelease também pode ser None, o que significa que uma característica planejada foi descartada ou que isso ainda não está decidido.

_Feature.compiler_flag

CompilerFlag é o sinalizador (bitfield) que deve ser passado no quarto argumento para a função embutida compile() para habilitar o recurso no código compilado dinamicamente. Este sinalizador é armazenado no atributo _Feature.compiler_flag em instâncias de _Feature.

Ver também

Instruções future

Como o compilador trata as importações de future.

PEP 236 - De volta ao __future__

A proposta original para o mecanismo do __future__.