2011-08-18 95 views
6

在解釋器中,您可以只寫一個對象的名稱,例如在這樣解釋提示列表a = [1, 2, 3, u"hellö"]Python解釋器中的>>> >>> some_object和print >>> some_object有什麼不同?

>>> a 
[1, 2, 3, u'hell\xf6'] 

,或者你可以這樣做:

>>> print a 
[1, 2, 3, u'hell\xf6'] 

這似乎等效名單。目前我正在使用hdf5來管理一些數據,並且我意識到上述兩種方法之間存在差異。鑑於:

with tables.openFile("tutorial.h5", mode = "w", title = "Some Title") as h5file: 
    group = h5file.createGroup("/", 'node', 'Node information') 
    tables.table = h5file.createTable(group, 'readout', Node, "Readout example") 

print h5file 

輸出從

>>> h5file 

不同,所以我想知道,如果有人可以解釋在這兩種情況下Python的行爲差異?

回答

8

鍵入一個對象到終端呼叫__repr__(),其是用於所述對象的詳細表示要打印(明確的)。當你告訴'打印'的東西時,你打電話給__str__(),因此要求一些可讀的東西。

亞歷克斯·馬爾泰利給了一個很好的解釋here。線程中的其他響應也可能會說明差異。

例如,看看datetime對象。

>>> import datetime 
>>> now = datetime.datetime.now() 

...比較

>>> now 
Out: datetime.datetime(2011, 8, 18, 15, 10, 29, 827606) 

到...

>>> print now 
Out: 2011-08-18 15:10:29.827606 

希望這使得它更有點清楚!

1

print方法總是調用x.__str__()方法while(只在交互式interpeter中)簡單地調用方法ia調用的變量對象x.__repr__()

>>> '\x02agh' 
'\x02agh' 
>>> print '\x02agh' 
'agh' 
+0

這是記錄在哪裏?我找不到它。 – jtbandes

+0

@jtbandes不知道在哪裏,但你可以自己測試一下。 PS:有人可以請求格式化內聯代碼嗎?它不適用於移動設備。謝謝 ! –

+0

當然,我可以測試它,這不是我問我:) – jtbandes

2

交互式解釋器將打印輸入到其中的每個表達式的結果。 (由於語句不評價,而是執行,這種打印方式並不適用於語句,如print本身,循環等)

證明repr()被使用的交互式解釋由尼克拉斯·羅森斯坦說(用2.6翻譯):

>>> class Foo: 
... def __repr__(self): 
...  return 'repr Foo' 
... def __str__(self): 
...  return 'str Foo' 
... 
>>> x = Foo() 
>>> x 
repr Foo 
>>> print x 
str Foo 

因此,儘管print聲明可能會在交互式解釋不必要的(除非你需要str,而不是repr),非交互式解釋並沒有這樣做。將上面的代碼放在一個文件中並運行該文件將導致沒有任何內容被打印。

+0

是的,x將被打印,因爲有一個打印語句;) –

+0

哈,這很有趣。我編輯了包含'print x'語句的答案,以表明使用'str'和另一個'repr'並忘記修改最後一段。 – wberry

0

,我們來看Python文檔:http://docs.python.org/reference/datamodel.html#object.repr

對象。 再版(個體)

Called by the repr() built-in function and by string conversions 

(反向引號)來計算 對象的「正式」的字符串表示。如果可能的話,這應該看起來像一個有效的Python 表達式,它可以用來重新創建一個具有相同 值的對象(給定一個適當的環境)。如果這是不可能的,一個 字符串的形式< ...一些有用的描述...>應該被返回。 返回值必須是一個字符串對象。如果類定義 再版(),但不STR(),然後再版()也用於當這個類的實例的 「非正式」字符串表示是需要 。

This is typically used for debugging, so it is important that the 

表示信息豐富且毫不含糊。

對象。 STR(個體)

Called by the str() built-in function and by the print statement 

來計算對象的「正規」的字符串表示。這個 不同於repr(),因爲它不一定是有效的Python 表達式:可以使用更方便或簡潔的表示法代替 。返回值必須是一個字符串對象。

例子:

>>> class A(): 
... def __repr__(self): return "repr!" 
... def __str__(self): return "str!" 
... 
>>> a = A() 
>>> a 
repr! 
>>> print(a) 
str! 
>>> class B(): 
... def __repr__(self): return "repr!" 
... 
>>> class C(): 
... def __str__(self): return "str!" 
... 
>>> b = B() 
>>> b 
repr! 
>>> print(b) 
repr! 
>>> c = C() 
>>> c 
<__main__.C object at 0x7f7162efb590> 
>>> print(c) 
str! 

打印功能打印每一個參數的控制檯__str__。像print(str(obj))

但在交互式控制檯中,它打印函數的返回值爲__repr__。如果__str__未定義,則可以使用__repr__

理想情況下,__repr__的意思是,我們應該使用該表示法來重現該對象。它不應該在不同的類之間相同,或者不同的對象表示不同的值。例如,datetime.time:

但是__str__(我們從str(obj)得到的)應該看起來不錯,因爲我們向用戶展示它。

>>> a = datetime.time(16, 42, 3) 
>>> repr(a) 
'datetime.time(16, 42, 3)' 
>>> str(a) 
'16:42:03' #We dont know what it is could be just print: 
>>> print("'16:42:03'") 
'16:42:03' 

而且,對不起英文不好:)。

0

print(variable)等於print(str(variable))

variable等於print(repr(variable))

顯然,物體h5file__repr____str__方法產生不同的結果。