Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I weak reference methods?
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/599430/why-doesnt-the-weakref-work-on-this-bound-method">Why doesn&#39;t the weakref work on this bound method?</a> </p> </blockquote> <p>A bit of context:</p> <p>I was trying to implement an Listener (or Observer, same thing) pattern: An EventManager keeps a list of all the Listeners handlers interested in an Event. For example, a Listener object would have a <code>onEndOfTheWorldEvent</code> method which would be called by the EventManager each time an instance of the event class EndOfTheWorldEvent is posted. Easy.</p> <p>Except that I wanted to weak reference the handlers because I don't want the EventManager to keep my handlers (bound methods) alive when the Listener was not needed anymore.</p> <p>So I thought "Let's throw all the handlers in a WeakSet". I couldn't get it to work.</p> <p>I dump here the code (or what's left of it when I reduce it to the minimum, here there's only one type of event and only one type of handler).</p> <pre><code>#! /usr/bin/python """ """ import sys import weakref class Listener(object): def handler(self, event): print event class EventManager(object): def __init__(self): self.handlers = weakref.WeakSet() def register(self, listener): print "Registering..." self.handlers.add(listener.handler) CountRefs(listener.handler) print "Number of handlers registered:", len(self.handlers) print "Registered." def CountRefs(what): print "Hard count:", sys.getrefcount(what) print "Weak count:", weakref.getweakrefcount(what) listener = Listener() em = EventManager() CountRefs(listener.handler) em.register(listener) CountRefs(listener.handler) </code></pre> <p>result:</p> <pre><code>Hard count: 3 Weak count: 0 Registering... Hard count: 3 Weak count: 0 Number of handlers registered: 0 Registered. Hard count: 3 Weak count: 0 </code></pre> <p>It just looks like there's never any weak reference, and the set remains empty.</p> <p>To make it even simpler:</p> <pre><code>&gt;&gt;&gt; class C(object): &gt;&gt;&gt; def blah(self): &gt;&gt;&gt; print "blah" &gt;&gt;&gt; &gt;&gt;&gt; c = C() &gt;&gt;&gt; w = weakref.ref(c.blah) &gt;&gt;&gt; print w &lt;weakref at 0x11e59f0; dead&gt; </code></pre> <p>Can't I create weakrefs to methods at all ? If not, <strong>why not</strong> ?</p> <p>So I guess a workaround would be to replace the WeakSet with a WeakKeyDictionary: key is the listener itself, and value the handler. Indeed I can weakref my Listeners. But it makes the data structure a bit more complicated, and when comes the time to broadcast the events to everybody, there's one more level in that structure to go through.</p> <p>What do you think ?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload