repr 的输出什么时候有用?

发布于 2024-10-11 15:38:49 字数 367 浏览 6 评论 0原文

我一直在阅读有关 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无敌元气妹 2024-10-18 15:38:49

理论上,repr(obj) 应该输出一个字符串,以便可以将其输入到 eval 中以重新创建对象。换句话说,

obj2 = eval(repr(obj1))

应该重现该对象。

实际上,repr 通常是 str 的“精简”版本。 str 可能会打印对象的人类可读形式,而 repr 则打印出对象的类等信息,通常用于调试目的。但有用性在很大程度上取决于您的情况以及相关对象如何处理 repr

Theoretically, repr(obj) should spit out a string such that it can be fed into eval to recreate the object. In other words,

obj2 = eval(repr(obj1))

should reproduce the object.

In practice, repr is often a "lite" version of str. str might print a human-readable form of the object, whereas repr 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 handles repr.

也只是曾经 2024-10-18 15:38:49

有时您必须处理或呈现一个字节字符串,例如

bob2='bob\xf0\xa4\xad\xa2'

如果您打印你得到的这个(在 Ubuntu 中)

In [62]: print(bob2)
bob

Sometimes you have to deal with or present a byte string such as

bob2='bob\xf0\xa4\xad\xa2'

If you print this out (in Ubuntu) you get

In [62]: print(bob2)
bob????

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 like bob𤭢. 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 (possibly utf-8 and cp1252 respectively), different results ensue.

In contrast, the repr of a string is unambiguous:

In [63]: print(repr(bob2))
'bob\xf0\xa4\xad\xa2'

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 object obj's __repr__ method. Since in your example the class A 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:

  1. that b is an instance of class A
    in the __main__
    namespace,
  2. and that the object resides in
    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 using id).

末蓝 2024-10-18 15:38:49

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文