Python中的'print'是什么?

61

我知道 print 做了什么,但它是哪种语言元素的"类型"呢?我认为它是一个函数,但为什么会失败呢?

>>> print print
SyntaxError: invalid syntax

print不是函数吗?它难道不应该打印出类似这样的东西吗?

>>> print print
<function print at ...>

print 可能是一个函数或语句 (语言结构),这取决于你所使用的 Python 的主要版本,但它绝对不是一个运算符。 - David Ferenczy Rogožan
5个回答

63

在2.7及以下版本中,print是一个语句。在Python 3中,print是一个函数。要在Python 2.6或2.7中使用print函数,可以这样做:

>>> from __future__ import print_function
>>> print(print)
<built-in function print>

请看 Python 语言参考手册中的 这一小节,以及 PEP 3105,了解为什么要进行更改。


15
请注意,上面的注释中“Note”拼错成了“Not” ;) - msw
终于修复了。损坏的打印语句是我过去没有使用Python的主要原因。 - Nate C-K
9
第一条评论并非指已被删除的评论。 - wim
4
但我并不知道第一条评论不能指的是我看不到它上面的那一条。 - Camilo Martin

35
在Python 3中,print()是一个内置函数(对象)。
在此之前,print是一个语句。演示...

Python 2.x:

% pydoc2.6 print

The ``print`` statement
***********************

   print_stmt ::= "print" ([expression ("," expression)* [","]]
                  | ">>" expression [("," expression)+ [","]])

``print`` evaluates each expression in turn and writes the resulting
object to standard output (see below).  If an object is not a string,
it is first converted to a string using the rules for string
conversions.  The (resulting or original) string is then written.  A
space is written before each object is (converted and) written, unless
the output system believes it is positioned at the beginning of a
line.  This is the case (1) when no characters have yet been written
to standard output, (2) when the last character written to standard
output is a whitespace character except ``' '``, or (3) when the last
write operation on standard output was not a ``print`` statement. (In
some cases it may be functional to write an empty string to standard
output for this reason.)

-----8<-----

Python 3.x:

% pydoc3.1 print

Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout)

    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file: a file-like object (stream); defaults to the current sys.stdout.
    sep:  string inserted between values, default a space.
    end:  string appended after the last value, default a newline.

在Python 3中,OP将会得到相同的输出,尽管原因不同;print print在两种情况下都是错误的语法。 - Wooble
@Wooble:完全正确。作为一个函数,print()需要括号。我在我的答案中包含了这些。 - johnsyweb
1
是的,我只是在指"你必须使用Python < 3"。错误信息在两者中都与OP的完全相同。 - Wooble
@Downvoter:我很好奇你认为我的回答哪些部分“没有用”。 - johnsyweb

21

print是Python 3中已经纠正的错误。在Python 3中,它是一个函数。而在Python 1.x和2.x中,它不是一个函数,它类似于ifwhile这样的特殊形式,但与这两者不同的是,它不是一种控制结构。

因此,我想最准确地称它为语句。


7
在Python中,除了赋值以外的所有语句都使用保留字而非可寻址对象表示。这就是为什么你不能简单地输入print print,并因尝试而得到SyntaxError。它是一个保留字,而不是一个对象。
令人困惑的是,你可以有一个名为print的变量。虽然你无法以正常方式访问它,但你可以使用setattr(locals(),'print',somevalue),然后使用print locals()['print']
其他一些可能作为变量名很理想但却被禁止的保留字包括:
class
import
return
raise
except
try
pass
lambda

“verboten” 在英语中使用?有趣。 - Jacob
@cularis:这个词可能因为关于二战的美国电影而成为更常用的外来词。 - Andrew Grimm
没有什么是混淆的; 在Python 2.6+中,一直存在内置的print函数。 from __future__ import print_function从模块中删除了“打印语句”解析,因此它不会干扰内置函数。 - Antti Haapala -- Слава Україні

1
在Python 2中,print是一个语句(statement),它与变量或函数完全不同。语句不是Python对象,不能传递给type();它们只是语言本身的一部分,甚至比内置函数还要重要。例如,你可以做sum = 5(虽然不应该这样做),但你不能做print = 5if = 7,因为printif是语句。
在Python 3中,print语句被print()函数替换。因此,如果你执行type(print),它将返回<class 'builtin_function_or_method'>
额外福利:
在Python 2.6+中,你可以在脚本顶部(作为第一行代码)加上from __future__ import print_function,这样print语句就会被替换为print()函数。
>>> # Python 2
>>> from __future__ import print_function
>>> type(print)
<type 'builtin_function_or_method'>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接