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

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

  • 호환되지 않는 변경 사항이 도입된 시점과 그것이 필수적일 때를 — 또는 이미 필수적으로 된 때를 — 문서로 만들기 위해. 이것은 실행 가능한 문서 형식이며, __future__ 를 임포트 해서 내용을 들여다봄으로써 프로그래밍 방식으로 검사 할 수 있습니다.

  • 파이썬 2.1 이전의 배포에서 퓨처 문 을 실행하면 최소한 실행시간 예외를 일으키도록 보장하기 위해 (2.1 이전에는 그런 이름의 모듈이 없으므로 __future__ 임포트는 실패합니다).

Module Contents

어떤 기능 설명도 __future__ 에서 삭제되지 않습니다. 파이썬 2.1에서 소개된 이후로 이 메커니즘을 사용하여 다음과 같은 기능이 언어에 도입되었습니다:

기능

선택적 버전

필수적 버전

효과

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: print를 함수로 만들기

unicode_literals

2.6.0a2

3.0

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

generator_stop

3.5.0b1

3.7

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

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.