python - 访问对象内存地址

标签 python object memory-address repr

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

<__main__.Test object at 0x2aba1c0cf890> 

如果你重载 __repr__() 有什么方法可以获取内存地址,然后调用 super(Class, obj).__repr__() 和正则表达式?

最佳答案

Python manual关于 id() 有这样的说法:

Return the "identity'' of an object. This is an integer (or long integer) which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value. (Implementation note: this is the address of the object.)

所以在 CPython 中,这将是对象的地址。但是,任何其他 Python 解释器都没有这样的保证。

请注意,如果您正在编写 C 扩展,则可以完全访问 Python 解释器的内部,包括直接访问对象的地址。

关于python - 访问对象内存地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/121396/

相关文章:

python - 有没有办法在 pygame 中控制网络摄像头的焦点?

python - Pygame 让一个圆围绕另一个圆旋转

python - 姓名或头衔方面

C++ 指针的内存地址和指针数组的内存地址如何相同?

c++ - 为什么我可以将大小不同的对象存储在数组中?

python - 我的 python 游戏代码无法运行

java - 从java程序中的其他类调用在一个类中实例化的对象

iphone - OAuth2Client #import "NXOAuth2.h"找不到文件

c++ - 如何用 1000000 个元素声明类 C++

java - java中对象的单纯名称意味着什么(Array,ArrayList)