为什么我们在导入print_function后调用print(在Python 2.6中) [英] why do we invoke print after importing print_function (in Python 2.6)
问题描述
要获得3.0打印功能,我们在Python 2.6中执行以下操作:
To get the 3.0 print function we do the following in Python 2.6:
from __future__ import print_function
但是要使用函数我们调用print()而不是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?
为什么不是以下内容:
from __future__ import print
推荐答案
原因是当您从 __ future __
导入时,您实际上只是设置一个标志,告诉解释器的行为与平时略有不同 - 在<$的情况下c $ c> print_function , print()
函数可用于代替语句。 __ future __
模块因此是特殊或魔术 - 它不像通常的模块那样工作。
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.
这篇关于为什么我们在导入print_function后调用print(在Python 2.6中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!