Program Tip

print_function을 가져온 후에 왜 print를 호출합니까 (Python 2.6에서)

programtip 2020. 12. 5. 10:30
반응형

print_function을 가져온 후에 왜 print를 호출합니까 (Python 2.6에서)


3.0 인쇄 기능을 얻으려면 Python 2.6에서 다음을 수행합니다.

from __future__ import print_function

그러나이 함수를 사용하려면 print_function ()이 아닌 print ()를 호출합니다. 이것은 단지 불일치입니까 아니면 이에 대한 정당한 이유가 있습니까?

다음은 왜 안됩니까?

from __future__ import print

그 이유는 당신이에서 가져올 때이다 __future__의 경우 - 당신은 정말 다르게 평소보다 조금 행동 인터프리터를 알려주는 플래그를 설정하는 print_functionprint()기능은 문 대신에 사용할 수 있습니다. 따라서 __future__모듈은 "특별한"또는 "마법"입니다. 일반적인 모듈처럼 작동하지 않습니다.


print_functionA는 FeatureName와 혼동하지 print내장 함수 자체. 제공 할 수있는 내장 기능을 사용할 수 있도록 향후부터 사용할 수있는 기능입니다.

기타 기능은 다음과 같습니다.

all_feature_names = [
    "nested_scopes",
    "generators",
    "division",
    "absolute_import",
    "with_statement",
    "print_function",
    "unicode_literals",
]

코드를 다음 상위 버전으로 마이그레이션 할 때 프로그램이 __future__버전 대신 업데이트 된 기능을 사용하는 것과 같은 특정 이유가 있습니다 . 또한 함수 이름이거나 키워드 자체 인 경우 파서에 혼란을 줄 수 있습니다.


Python 3에서는 키워드 print가 명령문 호출에서 함수 호출로 변경되었습니다.

그래서 그 대신 말하는 print value당신이 지금 말할 필요 print(value), 또는 당신이를 얻을 수 있습니다 SyntaxError.

를 수행하면 import이 변경 사항이 Python 2에서도 적용되므로 Python 3과 동일한 구문을 사용하여 프로그램을 작성할 수 있습니다 (적어도 print관련이 있음).


단순한. print는 Python 2의 키워드입니다.

그래서 같은 진술

from somewhere import print

Python 2에서는 자동 SyntaxError입니다.

허용 (구문에서 하드 코딩)

from __future__ import print

노력할 가치가없는 것으로 간주되었습니다.


최소한의 예

>>> print     # Statement.

>>> from __future__ import print_function
>>> print     # Function object.
<built-in function print>
>>> print()   # Function call.

>>>

언급했듯이 : 파이썬에서 __future__는 무엇에 사용되며 어떻게 / 언제 사용되며 어떻게 작동하는지 from __future__ 는 파이썬이 코드를 구문 분석하는 방법을 변경하는 마법의 문입니다.

from __future__ import print_function특히 print위의 대화 형 셸에 표시된 것처럼 명령문에서 내장 함수로 변경 됩니다.

Python 2에서 print(1)없이 작동하는 이유from __future__ import print_function

때문에 :

print(1)

다음과 같이 구문 분석됩니다.

print (1)
^^^^^ ^^^
1     2
  1. print 성명서
  2. 논의

대신에:

print( 1 )
^^^^^^ ^ ^
1      2 1
  1. print() 함수
  2. 논의

과:

assert 1 == (1)

as mentioned at: Python tuple trailing comma syntax rule


For completness, all the currently available features are:

+------------------+-------------+--------------+----------------------------------------------------+
|     feature      | optional in | mandatory in |                       effect                       |
+------------------+-------------+--------------+----------------------------------------------------+
| nested_scopes    | 2.1.0b1     |          2.2 | PEP 227: Statically Nested Scopes                  |
| generators       | 2.2.0a1     |          2.3 | PEP 255: Simple Generators                         |
| division         | 2.2.0a2     |          3.0 | PEP 238: Changing the Division Operator            |
| absolute_import  | 2.5.0a1     |          3.0 | PEP 328: Imports: Multi-Line and Absolute/Relative |
| with_statement   | 2.5.0a1     |          2.6 | PEP 343: The “with” Statement                      |
| print_function   | 2.6.0a2     |          3.0 | PEP 3105: Make print a function                    |
| unicode_literals | 2.6.0a2     |          3.0 | PEP 3112: Bytes literals in Python 3000            |
| generator_stop   | 3.5.0b1     |          3.7 | PEP 479: StopIteration handling inside generators  |
| annotations      | 3.7.0b1     |          4.0 | PEP 563: Postponed evaluation of annotations       |
+------------------+-------------+--------------+----------------------------------------------------+

참고URL : https://stackoverflow.com/questions/4560804/why-do-we-invoke-print-after-importing-print-function-in-python-2-6

반응형