__future__
— Future statement definitions¶
소스 코드: Lib/__future__.py
__future__
is a real module, and serves three purposes:
임포트 문을 분석하고 임포트하는 모듈을 발견하리라고 기대하는 기존 도구가 혼동하지 않게 하려고.
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).호환되지 않는 변경 사항이 도입된 시점과 그것이 필수적일 때를 — 또는 이미 필수적으로 된 때를 — 문서로 만들기 위해. 이것은 실행 가능한 문서 형식이며,
__future__
를 임포트 해서 내용을 들여다봄으로써 프로그래밍 방식으로 검사 할 수 있습니다.
__future__.py
의 각 문장은 다음과 같은 형식입니다:
FeatureName = _Feature(OptionalRelease, MandatoryRelease,
CompilerFlag)
보통, OptionalRelease 는 MandatoryRelease 보다 작으며, 둘 다 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
)
OptionalRelease 는 해당 기능이 승인된 첫 번째 배포를 기록합니다.
MandatoryRelease 가 아직 배포되지 않은 경우, MandatoryRelease 는 해당 기능이 언어 일부가 될 배포를 예측합니다.
그렇지 않으면 MandatoryRelease 는 기능이 언어 일부가 된 때를 기록합니다; 그 배포와 그 이후의 배포에서, 모듈이 해당 기능을 사용하기 위해 더 퓨처 문을 요구하지 않지만, 그러한 임포트는 계속 사용할 수 있습니다.
MandatoryRelease may also be None
, meaning that a planned feature got
dropped.
Instances of class _Feature
have two corresponding methods,
getOptionalRelease()
and getMandatoryRelease()
.
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 compiler_flag
attribute on _Feature
instances.
어떤 기능 설명도 __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 |
TBD 1 |
PEP 563: 어노테이션의 지연된 평가 |
- 1
from __future__ import annotations
was previously scheduled to become mandatory in Python 3.10, but the Python Steering Council twice decided to delay the change (announcement for Python 3.10; announcement for Python 3.11). No final decision has been made yet. See also PEP 563 and PEP 649.
더 보기
- 퓨처 문
컴파일러가 퓨처 임포트를 처리하는 방법.