[Python-Dev] Patching builtin_id to allow for C proxy objects?
greg.ewing at canterbury.ac.nz
Mon Jun 27 14:02:33 CEST 2011
Tom Whittock wrote:
> Currently every time one of these objects is accessed from Python, a
> new "myproxy" instance is created. So if I were to access the same
> field of an object twice, I would receive two python objects proxying
> the same underlying C++ object.
Perhaps you could use a WeakValueDictionary to keep a mapping
from a C++ object address to its Python proxy. Then as long as
a proxy object is alive, accessing the same C++ object again
will get you the same proxy object. When there are no longer
any references to the proxy object from Python, it will go
away. The next time you access that C++ object you'll get a
new proxy, but that won't matter, because the original proxy
is no longer around to compare it with.
This depends on there being some way for the proxy object
to ensure that the C++ object remains alive as long as it
It also won't solve the problem of keeping the id of the
proxy for longer than the proxy exists, but that's probably
not something you should be relying on anyway. The id of
*any* Python object is only valid while the object lives,
and if it's still alive you have a real reference somewhere
that you can use instead of the id for identity testing.
More information about the Python-Dev