What are the differences between _weak_ references and "normal" references?

Alan Kennedy alanmk at hotmail.com
Sat Apr 5 14:42:58 CEST 2003


sdieselil wrote:

> I noticed a module weakref in Python standard library. What is weak
> reference and in what way it differs from "normal" reference? By the
> way, how can I implement usual reference in Python (I mean C++
> reference)?

A good explanation of weak references, albeit with reference to that
other VM-based language, Java (ugh! ;-) can be read at the Sun website.

http://developer.java.sun.com/developer/technicalArticles/ALT/RefObj/index.html

Reading this article should greatly assist your understanding.

I've found weak references very useful (in Java) when implementing cache
mechanisms. When caching an object for later reuse (say file contents),
to save yourself the time and effort of reading and parsing the file
again, reference the object with a weak reference. If the VM runs out of
memory, it can claim the memory belonging to your object, because it is
only referenced with a weak reference. When you go to actually reference
the (reclaimed) object, you then discover that the object is gone, and
that you have to reload it.

Can anyone else enlighten us as to other uses for weak references?

Regards,

-- 
alan kennedy
-----------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan:              http://xhaus.com/mailto/alan




More information about the Python-list mailing list