为什么我们在导入 print_function 后调用 print(在 Python 2.6 中)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4560804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
why do we invoke print after importing print_function (in Python 2.6)
提问by H2ONaCl
To get the 3.0 print function we do the following in Python 2.6:
要获得 3.0 打印功能,我们在 Python 2.6 中执行以下操作:
from __future__ import print_function
But to use the function we invoke print() not print_function(). Is this just an inconsistency or is there a good reason for this?
但是要使用该函数,我们调用 print() 而不是 print_function()。这只是不一致还是有充分的理由?
Why not the following:
为什么不是以下内容:
from __future__ import print
采纳答案by kindall
The reason is that when you import from __future__you're really just setting a flag that tells the interpreter to behave a bit differently than usual -- in the case of print_function, the print()function is made available in place of the statement. The __future__module is thus "special" or "magic" -- it doesn't work like the usual modules.
原因是当你从你导入时,__future__你实际上只是设置一个标志,告诉解释器的行为与平常有所不同——在 的情况下print_function,该print()函数代替语句可用。__future__因此,该模块是“特殊的”或“神奇的”——它不像通常的模块那样工作。
回答by Tim Pietzcker
In Python 3, the keyword printhas been changed from calling a statement to calling a function.
在 Python 3 中,关键字print已从调用语句更改为调用函数。
So instead of saying print valueyou now need to say print(value), or you'll get a SyntaxError.
因此,与其说print value你现在需要说print(value),否则你会得到一个SyntaxError.
By doing the import, this change is effected in Python 2, too, so you can write programs using the same syntax as Python 3 (at least as far as printis concerned).
通过执行import,此更改也会在 Python 2 中生效,因此您可以使用与 Python 3 相同的语法编写程序(至少就目前print而言)。
回答by Senthil Kumaran
print_functionis a FeatureNamenot be confused with the printbuilt-in function itself.
It is a feature that is available from the future so that you can use the built-in function that it can provide.
print_function是一个FeatureName无法与混淆print内置函数本身。这是未来可用的功能,以便您可以使用它可以提供的内置功能。
Other Features include:
其他功能包括:
all_feature_names = [
"nested_scopes",
"generators",
"division",
"absolute_import",
"with_statement",
"print_function",
"unicode_literals",
]
There are specific reasons as when you migrate your code to next higher version, your program will remain as such as use the updated feature instead of the __future__version. Also if it were function name or the keyword itself, it may cause confusion to the parser.
有一些特定的原因,当您将代码迁移到下一个更高版本时,您的程序将保持使用更新的功能而不是__future__版本。此外,如果它是函数名称或关键字本身,则可能会导致解析器混淆。
回答by Andreas Kostyrka
Simple. print is keyword in Python 2.
简单的。print 是 Python 2 中的关键字。
So a statement like
所以像这样的声明
from somewhere import print
would be an automatic SyntaxError in Python 2.
将是 Python 2 中的自动 SyntaxError。
Allowing (hardcoding it in the syntax)
允许(在语法中对其进行硬编码)
from __future__ import print
was deemed not worth the effort.
被认为不值得付出努力。
回答by not2qubit
For completness, all the currently availablefeatures 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 |
+------------------+-------------+--------------+----------------------------------------------------+

