當前位置:首頁 » 編程語言 » python的repr

python的repr

發布時間: 2023-01-05 16:53:34

python 中 str 和 repr 的區別

函數str()用於將值轉化為適於人閱讀的形式,而repr()轉化為供解釋器讀取的形式內建函數str()和repr()(representation,表達,表示)或反引號操作符(``)可以方便地以字元串的方式獲取對象的內容、類型、數值屬性等信息。str()函數得到的字元串可讀性好(故被print調用)repr()函數得到的字元串通常可以用來重新獲得該對象,通常情況下obj==eval(repr(obj))這個等式是成立的。這兩個函數接受一個對象作為其參數,返回適當的字元串。事實上repr()和``做一樣的事情,返回一個對象的「官方」字元串表示。其結果絕大多數情況下(不是所有)可以通過求值運算(內建函數eval())重新得到該對象。str()則不同,它生成一個對象的可讀性好的字元串表示,結果通常無法用eval()求值,但適合print輸出。

② Python里str函數和repr函數有什麼區別

這個簡單
str是顯示給用戶用的
repr是給機器用的。
class
A(object):
def
__str__(self):
print
"this
is
A
class"
def
__repr__(self):
print
"this
is
repr
func"
a
=
A()
比如print
a
調用的是a的__str__方法
而如果你在python解釋器里直接敲a後回車,調用的是a.__repr__()方法

③ python __repr__的作用

1,嘗試生成這樣一個字元串,將其傳給 eval可重新生成同樣的對象 。

2,否則,生成用尖括弧包住的字元串,包含類型名和額外的信息。

④ Python 中 str 和 repr 的區別

盡管str(),repr()和``運算在特性和功能方面都非常相似,事實上repr()和``做的是完全一樣的事情,它們返回的是一個對象的「官方」字元串表示,也就是說絕大多數情況下可以通過求值運算(使用內建函數eval())重新得到該對象。

但str()則有所不同,str()致力於生成一個對象的可讀性好的字元串表示,它的返回結果通常無法用於eval()求值,但很適合用於print語句輸出。需要再次提醒的是,並不是所有repr()返回的字元串都能夠用 eval()內建函數得到原來的對象。 也就是說 repr() 輸出對 Python比較友好,而str()的輸出對用戶比較友好。

雖然如此,很多情況下這三者的輸出仍然都是完全一樣的。 大家可以看下下面的代碼,來進行對比
>>> s = 'Hello, world.'
>>> str(s)
'Hello, world.'
>>> repr(s)
"'Hello, world.'"
>>> str(0.1)
'0.1'
>>> repr(0.1)
'0.10000000000000001'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr(x) + ', and y is ' + repr(y) + '...'
>>> print s
The value of x is 32.5, and y is 40000...
>>> # The repr() of a string adds string quotes and backslashes:
... hello = 'hello, world\n'
>>> hellos = repr(hello)
>>> print hellos
'hello, world\n'
>>> # The argument to repr() may be any Python object:
... repr((x, y, ('spam', 'eggs')))
"(32.5, 40000, ('spam', 'eggs'))"

⑤ 菜鳥求大大們解釋Python里str函數和repr函數的區別

簡單來說
str()將數值轉成字元串
repr()將對象轉成字元串顯示,注意只是顯示,有些對象轉成字元串沒有意義。如list,dict使用str()是無效的,但使用repr可以,這是為了顯示他們的值

以下內容摘自google
The str() function is meant to return representations of values which are fairly
human-readable, while repr() is meant to generate representations which can be read by the interpreter (or will force a SyntaxError if there is not equivalent syntax). For
objects which don't have a particular representation for human consumption, str() will
return the same value as repr(). Many values, such as numbers or structures like lists
and dictionaries, have the same representation using either function. Strings and
floating point numbers, in particular, have two distinct representations.
Some examples:
>>> s = 'Hello, world.'
>>> str(s)
'Hello, world.'
>>> repr(s)
"'Hello, world.'"
>>> str(0.1)
'0.1'
>>> repr(0.1)
'0.10000000000000001'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr(x) + ', and y is ' + repr(y) + '...'
>>> print s
The value of x is 32.5, and y is 40000...
>>> # The repr() of a string adds string quotes and backslashes:
>>> hello = 'hello, world\n'
>>> hellos = repr(hello)
>>> print hellos
'hello, world\n'
>>> # The argument to repr() may be any Python object:
>>>repr((x, y, ('spam', 'eggs')))
"(32.5, 40000, ('spam', 'eggs'))"
>>> # reverse quotes are convenient in interactive sessions:
>>> `x, y, ('spam', 'eggs')`
"(32.5, 40000, ('spam', 'eggs'))"

⑥ python str和repr的區別

str與repr區別:

1、python中str函數通常把對象轉換成字元串,即生成對象的可讀性好的字元串,一般在輸出文本時使用,或者用於合成字元串。str的輸出對用戶比較友好適合print輸出。

2、pyton中repr函數將一個對象轉成類似源代碼的字元串,只用於顯示。repr的輸出對python友好,適合eval函數得到原來的對象。

3、在類中實現__str__和__repr__方法,就可以得到不同的返回,示例代碼:

>>>classtest(object):
def__repr__(self):
return"returntestrepr()string."
def__str__(self):
return"returnteststr()string."
>>>print(str(test()))
returnteststr()string.
>>>print(repr(test()))
returntestrepr()string.

⑦ python str和repr的區別

大體差不多,都是字元串化顯示,但是後者注重調試,會顯示「完整」的內容,可能會有不想要的細節(比如列印帶換行符的字元串,會列印「\n」而不是列印到換行符時換行)
str()是將作用對象「以字元串格式輸出」,重在輸出;repr()是「顯示對象是什麼東西」,重在表述。所以在調試程序時常常用後者列印。
具體應用時差別題主自己摸索吧。。。不可能列出來的。

⑧ Python中__str__和__repr__的區別

Python的默認實現往往是非常好用的,但是在這個例子中,如果有 __repr__ 的默認實現則會變為:
return "%s(%r)" % (self.__class__, self.__dict__)
這將會非常危險,如果有對象之間的相互引用,則會陷入無限的遞歸中,當然,Python有一個默認實現是如果 __repr__ 被定義了,但是 __str__ 沒有,則會實現為 __str__=__repr__ ,這意味簡單來說,你實現的每一個對象都應實現方法 __repr__ ,而 __str__ 是可選的,除非你希望有一個比較好的print效果。

任何重要系統都會有日誌系統,Python記錄日誌是非常簡單的,一般情況下會封裝為:
log(INFO, "I am in the weird function and a is", a, "and b is" , b, "but i got a null C - using default", default_c)
用戶通常需要為每一個對象實現repr方法,這樣這種記錄日誌的方法的就可以工作了。

具體來說,它不是為了准確,比如 str(3)==str("3") ,同樣地,當你實現IP地址時,可以使字元串表示為「192.168.1.1」,如果是實現一個日期則表示為"2020/4/12 14:32:23"更合適。它的目標是讓用戶而非開發者更可讀

在你實現任何的類中實現 __repr__ 方法,如果為了提高可讀性則可以實現 __str__ 方法

熱點內容
隨機啟動腳本 發布:2025-07-05 16:10:30 瀏覽:528
微博資料庫設計 發布:2025-07-05 15:30:55 瀏覽:25
linux485 發布:2025-07-05 14:38:28 瀏覽:305
php用的軟體 發布:2025-07-05 14:06:22 瀏覽:756
沒有許可權訪問計算機 發布:2025-07-05 13:29:11 瀏覽:432
javaweb開發教程視頻教程 發布:2025-07-05 13:24:41 瀏覽:707
康師傅控流腳本破解 發布:2025-07-05 13:17:27 瀏覽:241
java的開發流程 發布:2025-07-05 12:45:11 瀏覽:686
怎麼看內存卡配置 發布:2025-07-05 12:29:19 瀏覽:285
訪問學者英文個人簡歷 發布:2025-07-05 12:29:17 瀏覽:835