Accessing Object Memory Address Accessing Object Memory Address python python

Accessing Object Memory Address


The Python manual has this to say about 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.)

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

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.


You could reimplement the default repr this way:

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