[Python-ideas] weakrefs

Antoine Pitrou solipsis at pitrou.net
Thu May 17 17:44:29 CEST 2012


On Thu, 17 May 2012 08:10:40 -0700
Ethan Furman <ethan at stoneleaf.us> wrote:
>  From the manual [8.11]:
> 
>  > A weak reference to an object is not enough to keep the object alive:
>  > when the only remaining references to a referent are weak references,
>  > garbage collection is free to destroy the referent and reuse its
>  > memory for something else.
> 
> This leads to a difference in behaviour between CPython and the other 
> implementations:  CPython will (currently) immediately destroy any 
> objects that only have weak references to them with the result that 
> trying to access said object will require making a new one;

This is only true if the object isn't caught in a reference cycle.

> Without this stronger guarantee programs that are relying on weakrefs to 
> disappear when strong refs are gone end up relying on the gc method 
> instead, with the result that the program behaves differently on 
> different implementations.

Why would they "rely on weakrefs to disappear when strong refs are
gone"? What is the use case?

Regards

Antoine.





More information about the Python-ideas mailing list