non-owning references?

John Nagle nagle at animats.com
Sun Jul 26 01:52:18 EDT 2009


Peter Otten wrote:
> Utpal Sarkar wrote:
> 
>> Is there a way I can tell a variable that the object it is pointing
>> too is not owned by it, in the sense that if it is the only reference
>> to the object it can be garbage collected?
> 
> http://docs.python.org/library/weakref.html

    Yes.  Weak references can be quite useful.  I have a version of
BeautifulSoup in which all the "upward" and "backwards" links are
weak references, but the "downward" and "forwards" links are strong
references.  This breaks the loops in the data structure, and thus
trees and subtrees will go away when no longer required, without
requiring a GC cycle.

				John Nagle



More information about the Python-list mailing list