[Python-Dev] weakref enhancements

Greg Ewing greg.ewing at canterbury.ac.nz
Fri Sep 29 03:13:46 CEST 2006


Raymond Hettinger wrote:
> Also, I question the utility of maintaining a weakref to a method or 
> attribute instead of holding one for the object or class.  As long as 
> the enclosing object or class lives, so too will their methods and 
> attributes.  So what is the point of a tighter weakref granualarity?

I think you're misunderstanding what the OP means. A
weak attribute isn't a weak reference to an attribute,
it's an attribute that holds a weak reference and is
automatically dereferenced when you access it.

A frequent potential use case is parent-child relationships.
To avoid creating cycles you'd like to make the link
from child to parent weak, but doing that with raw
weakrefs is somewhat tedious and doesn't feel worth
the bother. If I could just declare the attribute
to be weak and then use it like a normal attribute
from then on, I would probably use this technique
more often.

> So, before being entertained for addition to the standard library, this 
> idea should probably first be posted as an ASPN recipe,

That's a reasonable idea.

--
Greg


More information about the Python-Dev mailing list