当你在Python中调用object.__repr__()方法时,你会得到类似这样的结果:

< __main__。测试对象位于0x2aba1c0cf890>

如果你重载__repr__(),除了调用super(Class, obj).__repr__()和reexing它,有没有任何方法来获得内存地址?


当前回答

你可以这样重新实现默认的repr:

def __repr__(self):
    return '<%s.%s object at %s>' % (
        self.__class__.__module__,
        self.__class__.__name__,
        hex(id(self))
    )

其他回答

只是为了响应Torsten,我无法在常规python对象上调用addressof()。此外,id(a) != addressof(a)。这是在CPython中,其他什么都不知道。

>>> from ctypes import c_int, addressof
>>> a = 69
>>> addressof(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: invalid type
>>> b = c_int(69)
>>> addressof(b)
4300673472
>>> id(b)
4300673392

只使用

id(object)

你可以使用内置的'str'类型的'partition'方法获取任何对象的内存地址/位置。

下面是一个使用它来获取对象内存地址的例子:

Python 3.8.3 (default, May 27 2020, 02:08:17)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> object.__repr__(1)
'<int object at 0x7ca70923f0>'
>>> hex(int(object.__repr__(1).partition('object at ')[2].strip('>'), 16))
0x7ca70923f0
>>>

在这里,我使用内置的“对象”类“__repr__”方法与一个对象/项目如1作为参数返回字符串的字符串,然后我分区将返回一个元组的字符串之前我提供的字符串,我提供的字符串,然后我提供的字符串在字符串,以及内存位置定位后的物体”,我能得到它分区的内存地址的部分。

然后,由于内存地址作为返回元组中的第三项返回,我可以用索引2从元组中访问它。但是,它在我获得的字符串中有一个直角括号作为后缀,所以我使用'strip'函数来删除它,这将返回不带尖括号的字符串。然后,我将结果字符串转换为以16为基数的整数,然后将其转换为十六进制数。

你可以通过以下方法得到适合这个目的的东西:

id(self)

如果__repr__重载,你可以考虑__str__来查看变量的内存地址。

下面是Moshe Zadka在StackOverflow中__repr__和__str__的细节。