如何将对象的地址包含在对象表示中,类似于默认__repr__
的工作方式?
>>> a=object()
>>> a
<object object at 0x1002c8090>
class Foo(object):
def __repr__(self):
return '<my stuff, at '+obj_address+'>' # how do I get object address?
答案 0 :(得分:8)
地址是十六进制对象的ID:
>>> o = object()
>>> repr(o)
'<object object at 0x1028ed080>'
>>> id(o)
4337881216
>>> hex(id(o))
'0x1028ed080'
答案 1 :(得分:4)
class Foo(object):
def __repr__(self):
return '<my stuff, at 0x%x>' % id(self)