repr 的输出什么时候有用?
我一直在阅读有关 Python 中的 repr
的内容。我想知道 repr
的输出的应用是什么。例如
class A:
pass
repr(A) ='<class __main__.A at 0x6f570>'
b=A()
repr(b) = '<__main__.A instance at 0x74d78>'
,人们什么时候会对'
或'<__main__.A instance at 0x74d78>'
感兴趣?
I have been reading about repr
in Python. I was wondering what the application of the output of repr
is. e.g.
class A:
pass
repr(A) ='<class __main__.A at 0x6f570>'
b=A()
repr(b) = '<__main__.A instance at 0x74d78>'
When would one be interested in '<class __main__.A at 0x6f570>'
or'<__main__.A instance at 0x74d78>'
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
理论上,repr(obj) 应该输出一个字符串,以便可以将其输入到 eval 中以重新创建对象。换句话说,
应该重现该对象。
实际上,
repr
通常是str
的“精简”版本。str
可能会打印对象的人类可读形式,而repr
则打印出对象的类等信息,通常用于调试目的。但有用性在很大程度上取决于您的情况以及相关对象如何处理repr
。Theoretically,
repr(obj)
should spit out a string such that it can be fed intoeval
to recreate the object. In other words,should reproduce the object.
In practice,
repr
is often a "lite" version ofstr
.str
might print a human-readable form of the object, whereasrepr
prints out information like the object's class, usually for debugging purposes. But the usefulness depends a lot on your situation and how the object in question handlesrepr
.有时您必须处理或呈现一个字节字符串,例如
如果您打印你得到的这个(在 Ubuntu 中)
Sometimes you have to deal with or present a byte string such as
If you print this out (in Ubuntu) you get
which is not very helpful to others trying to understand your byte string. In the comments, John points out that in Windows,
print(bob2)
results in something likebobð¤¢
. The problem is that Python detects the default encoding of your terminal/console and tries to decode the byte string according to that encoding. Since Ubuntu and Windows uses different default encodings (possiblyutf-8
andcp1252
respectively), different results ensue.In contrast, the repr of a string is unambiguous:
When people post questions here on SO about Python strings, they are often asked to show the repr of the string so we know for sure what string they are dealing with.
In general, the repr should be an unambiguous string representation of the object.
repr(obj)
calls the objectobj
's__repr__
method. Since in your example the classA
does not have its own__repr__
method,repr(b)
resorts to indicating the class and memory address.You can override the
__repr__
method to give more relevant information.In your example,
'<__main__.A instance at 0x74d78>'
tells us two useful things:b
is an instance of classA
in the
__main__
namespace,
memory at address 0x74d78.
You might for instance, have two instances of class
A
. If they have the same memory address then you'd know they are "pointing" to the same underlying object. (Note this information can also be obtained usingid
).repr()
的主要用途是在交互式解释器和调试器中使用它以人类可读的形式格式化对象。您给出的示例主要用于调试目的。The main purpose of
repr()
is that it is used in the interactive interpreter and in the debugger to format objects in human-readable form. The example you gave is mainly useful for debugging purposes.