title: How and when to appropriately use weakref in Python
body: <p>I have some code where instances of classes have parent&lt;-&gt;child references to each other, e.g.:</p>
  <pre><code>class Node:
  
      def __init__(self):
          self.parent = None
          self.children = {}
  
      def AddChild(self, name, child):
          child.parent = self
          self.children[name] = child
  
  
  def Run():
      root, c1, c2 = Node(), Node(), Node()
      root.AddChild('first', c1)
      root.AddChild('second', c2)
  
  
  Run()
  </code></pre>
  <p>I <em>think</em> this creates circular references such that <code>root</code>, <code>c1</code> and <code>c2</code> won't be freed after Run() is completed, right?.  So, how do get them to be freed?  I think I can do something like <code>root.children.clear()</code>, or <code>self.parent = None</code> - but what if I don't know when to do that?</p>
  <p>Is this an appropriate time to use the weakref module?  What, exactly, do I weakref'ify?  the <code>parent</code> attribute?  The <code>children</code> attribute?  The whole object?  All of the above?  I see talk about the WeakKeyDictionary and weakref.proxy, but its not clear to me how they should be used, if at all, in this case.</p>
  <p>This is also on Python 2.4 (can't upgrade).</p>
  <p><strong>Update: Example and Summary</strong></p>
  <p>What objects to weakref-ify depends on which object can live without the other, and what objects depend on each other.  The object that lives the longest should contain weakrefs to the shorter-lived objects.  Similarly, weakrefs should not be made to dependencies - if they are, the dependency could silently disappear even though it is still needed.</p>
  <p>If, for example, you have a tree structure, <code>root</code>, that has children, <code>kids</code>, but can exist <em>without</em> children, then the <code>root</code> object should use weakrefs for its <code>kids</code>.  This is also the case if the child object depends on the existence of the parent object.  Below, the child object <em>requires</em> a parent in order to compute its depth, hence the strong-ref for <code>parent</code>.  The members of the <code>kids</code> attribute are optional, though, so weakrefs are used to prevent a circular reference.</p>
  <pre><code>class Node:
  
      def __init__(self):
          self.parent = None
          self.kids = weakref.WeakValueDictionary()
  
      def GetDepth(self):
          root, depth = self, 0
          while root:
              depth += 1
              root = root.parent
          return depth
  
  
  root = Node()
  root.kids['one'] = Node()
  root.kids['two'] = Node()
  </code></pre>
  <p>To flip the relationship around, we have something like the below.  Here, the <code>Facade</code> classes require a <code>Subsystem</code> instance to work, so they use a strong-ref to the subsystem they need.  <code>Subsystem</code>s, however, don't require a <code>Facade</code> to work.  <code>Subsystem</code>s just provide a way to notify <code>Facade</code>s about each other's actions.</p>
  <pre><code>class Facade:
  
    def __init__(self, subsystem):
      self.subsystem = subsystem
      subsystem.Register(self)
  
  
  class Subsystem:
  
      def __init__(self):
          self.notify = []
  
      def Register(self, who):
          self.notify.append(weakref.proxy(who))
  
  
  sub = Subsystem()
  cli = Facade(sub)
  </code></pre>