gpt4 book ai didi

python - 访问对象内存地址

转载 作者:IT老高 更新时间:2023-10-28 12:25:26 25 4
gpt4 key购买 nike

当你在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com