How do you create a weak reference to an object in Python?
<p>How do you create a weak reference to an object in Python?  </p>

<pre><code>&gt;&gt;&gt; import weakref
&gt;&gt;&gt; class Object:
...     pass
...
&gt;&gt;&gt; o = Object()
&gt;&gt;&gt; r = weakref.ref(o)
&gt;&gt;&gt; # if the reference is still active, r() will be o, otherwise None
&gt;&gt;&gt; do_something_with_o(r())
</code></pre>

<p>See the <a href="http://docs.python.org/lib/module-weakref.html">wearkref module docs</a> for more details.
You can also use <code>weakref.proxy</code> to create an object that proxies o. Will throw <code>ReferenceError</code> if used when the referent is no longer referenced.</p>
