当前位置:首页 » 编程语言 » 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