Top-rated recipes tagged "weakref"http://code.activestate.com/recipes/tags/weakref/top/2013-07-29T05:35:55-07:00ActiveState Code RecipesCalling (C-level) finalizers without __del__ (Python) 2011-07-02T18:42:08-07:00Benjamin Petersonhttp://code.activestate.com/recipes/users/4170802/http://code.activestate.com/recipes/577242-calling-c-level-finalizers-without-__del__/ <p style="color: grey"> Python recipe 577242 by <a href="/recipes/users/4170802/">Benjamin Peterson</a> (<a href="/recipes/tags/ctypes/">ctypes</a>, <a href="/recipes/tags/finalization/">finalization</a>, <a href="/recipes/tags/weakref/">weakref</a>). Revision 3. </p> <p>This recipe is meant for Python classes wrapping ctypes bindings where a C-level finalizer must be invoked when the wrapper is destroyed. It uses weakref callbacks to avoid problems with <code>__del__</code>.</p> Bound Method Weakref (Python) 2013-07-29T05:35:55-07:00Esteban Castro Borsanihttp://code.activestate.com/recipes/users/4184010/http://code.activestate.com/recipes/578298-bound-method-weakref/ <p style="color: grey"> Python recipe 578298 by <a href="/recipes/users/4184010/">Esteban Castro Borsani</a> (<a href="/recipes/tags/oop/">oop</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/weakref/">weakref</a>). Revision 4. </p> <p>This is a workaorund for weak reference bound methods. It may be useful for observer patterns.</p> weak reference map (Python) 2011-02-20T16:26:38-08:00Hui Zhanghttp://code.activestate.com/recipes/users/4177055/http://code.activestate.com/recipes/577580-weak-reference-map/ <p style="color: grey"> Python recipe 577580 by <a href="/recipes/users/4177055/">Hui Zhang</a> (<a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/weakref/">weakref</a>). Revision 2. </p> <p>keep (key, value)s in a map.</p> <p>if key or value was garbage collected, remove the corresponding records.</p> Yet another signal/slot implementation in Python (Python) 2008-09-01T23:21:28-07:00Thiago Marcos P. Santoshttp://code.activestate.com/recipes/users/4166797/http://code.activestate.com/recipes/576477-yet-another-signalslot-implementation-in-python/ <p style="color: grey"> Python recipe 576477 by <a href="/recipes/users/4166797/">Thiago Marcos P. Santos</a> (<a href="/recipes/tags/observer/">observer</a>, <a href="/recipes/tags/publish/">publish</a>, <a href="/recipes/tags/signal/">signal</a>, <a href="/recipes/tags/slot/">slot</a>, <a href="/recipes/tags/subscribe/">subscribe</a>, <a href="/recipes/tags/weakref/">weakref</a>). </p> <p>This code snippet was based on the nice <a href="http://code.activestate.com/recipes/439356/">recipe 439356</a> made by Patrick Chasco. My implementation supports only class methods callbacks. I'm keeping the idea of use weakrefs to avoid the interpreter keep the object allocated because the signal is registered (i.e. the signal object holds a reference to callback method). IMO the usage of WeakValueDictionary made the code smaller and clear and also are maintenance-free (when the object is collect by the garbage collector the signal is automatically unregistered). </p>