[Python-Dev] ctypes: is it intentional that id() is the only way to get the address of an object?

Antoine Pitrou solipsis at pitrou.net
Sat Jan 19 06:30:28 EST 2019


On Sat, 19 Jan 2019 13:28:06 +1300
Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> Tim Peters wrote:
> 
> > The dict itself keeps the objects alive.  
> 
> Yes, but the idea of a cache is that you're free to flush
> things out of it to make room for something else without
> breaking anything.
> 
> It sounds like MRAB is using ids as weak references,
> without the assurance actual weak references give you
> that they become invalidated when the refefenced object
> goes away,

Hmm...  That sounds nonsensical to me. By construction, if you're able
to get a reference to an object in pure Python, then the object is
alive.

(by pure Python I'm excluding ctypes hacks or the exploitation of bugs
in the CPython object implementation)

By the way, you can also have a WeakValueDictionary where keys are ids
and values are the corresponding objects, if you need both identity
lookup and weak references.

Regards

Antoine.




More information about the Python-Dev mailing list