what is the use of weakref?

Mike C. Fletcher mcfletch at rogers.com
Fri Jan 30 10:08:18 CET 2004

Well, for the last few versions of Python, it's primary use has been 
finding ways to crash the interpreter ;) .  Seriously, they are used 
when you want to avoid having cycles in your data structures, so that, 
for instance, variables will be immediately garbage collected rather 
than hanging around waiting for the garbage collector to find them.

They are also useful in some decoration patterns where you want to cache 
compiled values for a large number of objects with uncertain lifetimes.  
If you were to use hard-refs to the client objects from the cache you'd 
keep the objects alive (presumably the cache is reachable all the time). 
A weak-ref allows the client objects to go away so that the cached 
values can be cleared and the memory reclaimed (OpenGLContext does this 
with compiled scenegraph node representations, btw).


ali wrote:

>i've seen a lot of times in programs but until now i still dont know the use
>of the weakref module... any help will be appreciated... thanks...
  Mike C. Fletcher
  Designer, VR Plumber, Coder

More information about the Python-list mailing list