2011-08-11 39 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'>