访问对象存储器地址 [英] Accessing Object Memory Address

查看:83
本文介绍了访问对象存储器地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您在Python中调用object.__repr__()方法时,您会得到类似以下的信息:

When you call the object.__repr__() method in Python you get something like this back:

<__main__.Test object at 0x2aba1c0cf890> 

如果重载__repr__(),然后调用super(Class, obj).__repr__()并将其重新表达出来,是否有任何方法可以保留内存地址?

Is there any way to get a hold of the memory address if you overload __repr__(), other then calling super(Class, obj).__repr__() and regexing it out?

推荐答案

Python手册关于id()的说法:

返回对象的身份". 这是一个整数(或长整数) 保证是唯一的 此对象在其期间的常量 一生.两个对象 非重叠的生命周期可能具有 相同的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解释器都没有这样的保证.

So in CPython, this will be the address of the object. No such guarantee for any other Python interpreter, though.

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

Note that if you're writing a C extension, you have full access to the internals of the Python interpreter, including access to the addresses of objects directly.

这篇关于访问对象存储器地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆