weakattr (weak attributes) are attributes that are weakly referenced<br>by their containing object. they are very useful for cyclic references --<br>an object that holds a reference to itself. <br><br>when a cyclic reference is found by the GC, the memory may be
<br>freed, but __del__ is not called, because it's impossible to tell which <br>__del__ to call first. this is an awkward asymmetry with no clean <br>solution: most such objects provide a "close" or "dispose" method
<br>that must be called explicitly.<br><br>weakattrs to solve this problem, by providing a "magical" attribute<br>that "disappears" when the attribute is no longer strongly-referenced. <br><br>you can find the code, as well as some examples, on this link
<br><a href="http://sebulba.wikispaces.com/recipe+weakattr">http://sebulba.wikispaces.com/recipe+weakattr</a><br>
<br>since the stdlib already comes with weakref.py, which provides<br>higher level concepts over the builtin _weakref module, i'd like to<br>make weakattr a part of it. <br><br>it's only ~20 lines of code, and imho saves the trouble of explicitly
<br>releasing the resource of un__del__able objects.<br><br>i think it's useful. here's a snippet:<br><br>>>> from weakref import weakattr<br>>>> <br>>>> class blah(object):<br>... yada = weakref()
<br>...<br>>>> o1 = blah()<br>>>> o2 = blah()<br>>>> o1.yada = o2<br>>>> o2.yada = o1<br><br>o1.yada is a *weakref* to o2, so that when o2 is no longer <br>strongly-referenced...<br>>>> del o2
<br>o1.yada "magically" disappears as well. <br>>>> o1.yada<br>... AttributeError(...)<br><br>since the programmer explicitly defined "yada" as a weakatt, he/she <br>knows it might "disappear". it might look awkward at first, but that's
<br>exactly the *desired* behavior (otherwise we'd just use the regular <br>strong attributes).<br><br>another thing to note is that weakattrs are likely to be gone only<br>when the object's __del__ is already invoked, so the only code that
<br>needs to take such precautions is __del__ (which already has some <br>constraints)<br><br>for example:<br><br>>>> class blah(object):<br>... me = weakattr()<br>...<br>... def __init__(self):<br>...
self.me = self<br>...<br>... def something(self):<br>... # we can rest assure me exists at this stage<br>... print self.me<br>...<br>... def __del__(self):<br>... # by the time __del__ is called, "me" is removed
<br>... print "me exists?", hasattr(self, "me")<br>...<br>>>> b = blah()<br>>>> b.me<br><__main__.blah object at 0x00C0EC10><br>>>> b.something()<br><__main__.blah object at 0x00C0EC10>
<br>>>> del b<br>>>> import gc<br>>>> gc.collect()<br>me exists? False<br>0<br>>>><br><br><br><br>-tomer<br>