python 中重写 __str__ 方法后如何获取实例的地址

发布于 2024-11-13 04:42:31 字数 375 浏览 1 评论 0原文

class Bar:
     pass

class Foo:
     def __str__(self): return "Foo instance"

>> aBar = Bar()
>> print aBar
<__main__.Bar instance at 0x100572a28>
>> aFoo = Foo()
>> print aFoo
Foo instance

有没有办法在重写 str 方法后打印出 aFoo 的地址?

使用

 >>repr(aFoo) 

解决了我的问题

class Bar:
     pass

class Foo:
     def __str__(self): return "Foo instance"

>> aBar = Bar()
>> print aBar
<__main__.Bar instance at 0x100572a28>
>> aFoo = Foo()
>> print aFoo
Foo instance

is there a way to print out the address of aFoo after overriding the str method?

using

 >>repr(aFoo) 

solved my problem

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

陌上芳菲 2024-11-20 04:42:31

至少在cpython中,id提供了地址。但输出是十进制的;您必须将其转换为十六进制:

>>> f = (x for x in [1,2,3])
>>> print f
<generator object <genexpr> at 0x1004d22d0>
>>> '%x' % id(f)
'1004d22d0'

但实际上,当覆盖 __str__ 时,__repr__ 函数不会改变。所以你也可以这样做:

>>> class Foo:
...     def __str__(self): return "Foo instance"
... 
>>> a = Foo()
>>> print a
Foo instance
>>> print repr(a)
<__main__.Foo instance at 0x1004d1c68>

我认为如果你想要的确实是id,那么id更适合这个。但 id 不保证返回地址;这只是 cpython 的实现。我不知道是否指定对象的内置 __repr__ 必须返回地址,或者是否必须返回 id,或者两者都不返回。因此,如果您特别想要 __repr__ 提供的任何内容,那么这可能是正确的选择。

更新:答案都不是,至少根据语言参考,仅规定对象的 __repr__ 是“信息丰富且明确的”。事实上,有时 __repr__ 实际上并不返回相关特定对象的地址,如下所示:

>>> a = Foo()
>>> '%x' % id(a)
'1004d1fc8'
>>> '%x' % id(a.__str__)
'1004745a0'
>>> '%x' % id(Foo.__str__)
'1004745a0'
>>> repr(a.__str__)
'<bound method Foo.__str__ of <__main__.Foo instance at 0x1004d1fc8>>'

At least in cpython, id provides the address. But the output is in decimal; you have to convert that to hex:

>>> f = (x for x in [1,2,3])
>>> print f
<generator object <genexpr> at 0x1004d22d0>
>>> '%x' % id(f)
'1004d22d0'

Actually, though, the __repr__ function isn't altered when __str__ is overridden. So you can do this as well:

>>> class Foo:
...     def __str__(self): return "Foo instance"
... 
>>> a = Foo()
>>> print a
Foo instance
>>> print repr(a)
<__main__.Foo instance at 0x1004d1c68>

I think id is preferable for this, if what you want is really the id. But id is not guaranteed to return the address; that's just the cpython implementation. I don't know whether it's specified that the built-in __repr__ of objects has to return an address, or whether it has to return the id, or neither. So if you specifically want whatever it is that __repr__ provides, then this may be the way to go.

Update: The answer is neither, at least according to the language reference, which dictates only that the __repr__ of an object be "information-rich and unambiguous." And indeed, sometimes the __repr__ does not actually return the address of the specific object in question, as seen here:

>>> a = Foo()
>>> '%x' % id(a)
'1004d1fc8'
>>> '%x' % id(a.__str__)
'1004745a0'
>>> '%x' % id(Foo.__str__)
'1004745a0'
>>> repr(a.__str__)
'<bound method Foo.__str__ of <__main__.Foo instance at 0x1004d1fc8>>'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文