简体   繁体   English

python的打印功能不完全是普通的功能吗?

[英]python's print function not exactly an ordinary function?

Environment: python 2.x 环境:python 2.x

If print is a built-in function, why does it not behave like other functions ? 如果print是一个内置的功能,为什么它不能表现得像其他功能? What is so special about print ? print什么特别之处?

-----------start session--------------
>>> ord 'a'
Exception : invalid syntax
>>> ord('a')
97
>>> print 'a'
a
>>> print('a') 
a
>>> ord
<built-in function ord>
>>> print

-----------finish session--------------

print in Python versions below 3, is not a function. 在3以下的Python版本中print ,不是一个函数。 There's a separate print statement which is part of the language grammar. 有一个单独的print语句 ,它是语言语法的一部分。 print is not an identifier. print不是标识符。 It's a keyword. 这是一个关键字。

The short answer is that in Python 2, print is not a function but a statement . 简短的回答是,在Python 2中, print不是函数,而是声明

In all versions of Python, almost everything is an object. 在所有版本的Python中, 几乎所有东西都是一个对象。 All objects have a type. 所有对象都有一个类型。 We can discover an object's type by applying the type function to the object. 我们可以通过将type函数应用于对象来发现对象的类型。

Using the interpreter we can see that the builtin functions sum and ord are exactly that in Python's type system: 使用解释器,我们可以看到内置函数sumord正是Python类型系统中的函数:

>>> type(sum)
<type 'builtin_function_or_method'>
>>> type(ord)
<type 'builtin_function_or_method'>

But the following expression is not even valid Python: 但是下面的表达式甚至不是有效的Python:

>>> type(print)
SyntaxError: invalid syntax

This is because the name print itself is a keyword, like if or return . 这是因为名称print本身是一个关键字,如ifreturn Keywords are not objects. 关键字不是对象。

The more complete answer is that print can be either a statement or a function depending on the context. 更完整的答案是print可以是语句,也可以是函数,具体取决于上下文。

In Python 3, print is no longer a statement but a function . 在Python 3中, print不再是一个语句而是一个函数

In Python 2, you can replace the print statement in a module with the equivalent of Python 3's print function by including this statement at the top of the module: 在Python 2,你可以更换print与一个模块中的声明等同 Python 3中的的print功能,通过包括在模块的顶部这样的说法:

from __future__ import print_function

This special import is available only in Python 2.6 and above. 此特殊导入仅适用于Python 2.6及更高版本。

Refer to the documentation links in my answer for a more complete explanation. 有关更完整的说明,请参阅我的答案中的文档链接。

The deal is that print is built-in function only starting from python 3 branch. 这笔交易是print只是从python 3分支开始的内置函数。 Looks like you are using python2. 看起来你正在使用python2。

Check out: 查看:

print "foo"; # Works in python2, not in python3
print("foo"); # Works in python3

print is more treated like a keyword than a function in python. print更像是关键字而不是python中的函数。 The parser "knows" the special syntax of print (no parenthesis around the argument) and how to deal with it. 解析器“知道”print的特殊语法(参数周围没有括号)以及如何处理它。 I think the Python creator wanted to keep the syntax simple by doing so. 我认为Python创建者希望通过这样做来保持语法简单。 As maverik already mentioned, in python3 print is being called like any other function and a syntx error is being thrown if you do it the old way. 正如maverik已经提到的那样,在python3中,print会被调用,就像任何其他函数一样,如果以旧的方式执行,则会抛出合成错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM