2011-08-11 40 views
61

我明白print做什么,但是什么“类型”是那个语言元素?我认为这是一个功能,但为什么这会失败?什么是Python中的“打印”?

>>> print print 
SyntaxError: invalid syntax 

是不是print的功能?它不应该打印这样的东西吗?

>>> print print 
<function print at ...> 
+0

'print'可能是一个函数或语句(语言结构),这取决于Python中的主要版本,你正在使用,但它绝对不是一个运营商。 –

回答

63

在2.7和下,print是一个声明。在python 3中,print是一个函数。要使用在Python 2.6或2.7的打印功能,你可以做

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

为什么它改变见this section从Python语言参考,以及PEP 3105

+15

请注意,上面注释中的“注释”拼写错误为“Not”;) – msw

+0

大约他们修正了这个问题。破碎的打印语句是过去未使用过Python的第一个原因。 –

+8

不是说第一个评论以前提到了一个现在被删除的评论。 – wim

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 
+0

“verboten”用于英语?滑稽。 – Jacob

+0

@cularis:由于关于第二次世界大战的美国电影,这个词可能变得更常用作贷款用词。 –

+0

没有什么让人困惑的;在Python 2.6+中,一直都有内置的'print'功能。 'from __future__ import print_function'从模块中删除了'print-the-statement'解析,所以它不会干扰内置函数。 –

34

在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. 
+8

+1:提及pydoc – Kracekumar

+0

在Python 3中,OP会得到相同的输出,尽管原因不同; 'print print'在两者中都是不好的语法。 – geoffspear

+0

@Wooble:绝对。作为一个函数,'print()'需要括号。我在我的答案中包含了这些。 – Johnsyweb

2

在Python 2,print是一个声明,这是一个完全不同的来自变量或函数的东西。语句不是可传递给type()的Python对象;它们只是语言本身的一部分,甚至比内置函数还要多。例如,你可以做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'>