[Python-3000] weakrefs and cyclic references

Greg Ewing greg.ewing at canterbury.ac.nz
Fri Jun 2 03:14:32 CEST 2006

tomer filiba wrote:

> you can solve the problem using
> weakref.proxy
> ...
> so why not do this automatically?

I would *not* want to have some of my references chosen
at random and automatically made into weak ones. I may
temporarily create a cycle and later break it by removing
one of the references. If the other remaining reference had
been picked for auto-conversion into a weak reference, I
would lose the last reference to my object.

(Besides being undesirable, it would also be extremely
difficult to implement efficiently.)

What might be useful is an easier way of *explicitly*
creating and using weak references.

We already have WeakKeyDictionary and WeakValueDictionary
which behave just like ordinary dicts except that they
weakly reference things. I'm thinking it would be nice
to have a way of declaring any attribute to be a weak
reference. Then it could be read and written it in the
usual way, without all the code that uses it having
to know about its weakness.

This could probably be done fairly easily with a suitable
property descriptor.


More information about the Python-3000 mailing list