__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:

  • 임포트 문을 분석하고 임포트하는 모듈을 발견하리라고 기대하는 기존 도구가 혼동하지 않게 하려고.

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

Module Contents

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:

기능

선택적 버전

필수적 버전

효과

__future__.nested_scopes

2.1.0b1

2.2

PEP 227: 정적으로 중첩된 스코프

__future__.generators

2.2.0a1

2.3

PEP 255: 단순 제너레이터

__future__.division

2.2.0a2

3.0

PEP 238: 나누기 연산자 변경

__future__.absolute_import

2.5.0a1

3.0

PEP 328: 임포트: 복수 줄 및 절대/상대

__future__.with_statement

2.5.0a1

2.6

PEP 343: The “with” Statement

__future__.print_function

2.6.0a2

3.0

PEP 3105: print를 함수로 만들기

__future__.unicode_literals

2.6.0a2

3.0

PEP 3112: 파이썬 3000의 바이트열 리터럴

__future__.generator_stop

3.5.0b1

3.7

PEP 479: 제너레이터 내부의 StopIteration 처리

__future__.annotations

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)

보통, OptionalReleaseMandatoryRelease 보다 작으며, 둘 다 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 는 기능이 언어 일부가 된 때를 기록합니다; 그 배포와 그 이후의 배포에서, 모듈이 해당 기능을 사용하기 위해 더 퓨처 문을 요구하지 않지만, 그러한 임포트는 계속 사용할 수 있습니다.

MandatoryReleaseNone 일 수도 있습니다. 이는 계획된 기능이 삭제되었거나 아직 결정되지 않았음을 의미합니다.

_Feature.compiler_flag

CompilerFlag 은 동적으로 컴파일되는 코드에서 해당 기능을 활성화하기 위해, 내장 함수 compile() 의 네 번째 인자로 전달되어야 하는 (비트 필드) 플래그입니다. 이 플래그는 _Feature 인스턴스의 _Feature.compiler_flag 어트리뷰트에 저장됩니다.

더 보기

퓨처 문

컴파일러가 퓨처 임포트를 처리하는 방법.

PEP 236 - Back to the __future__

The original proposal for the __future__ mechanism.