2

Possible Duplicate:
Why doesn't the weakref work on this bound method?

I'm using weakrefs in an observer-pattern and noticed an interesting phenomenon. If I create an object and add one of it's methods as an observer of an Observable, the reference is dead almost instantly. Can anyone explain what is happening?

I'm also interested in thoughts for why this might be a bad idea. I've decided not to use the weakrefs and just make sure to clean up after myself properly with Observable.removeobserver, but my curiosity is killing me here.

Here's the code:

from weakref import ref
class Observable:
    __observers = None
    def addobserver(self, observer):
        if not self.__observers:
            self.__observers = []
        self.__observers.append(ref(observer))
        print 'ADDING observer', ref(observer)

    def removeobserver(self, observer):
        self.__observers.remove(ref(observer))

    def notify(self, event):
        for o in self.__observers:
            if o() is None:
                print 'observer was deleted (removing)', o
                self.__observers.remove(o)
            else:
                o()(event)

class C(Observable):
    def set(self, val):
        self.notify(val)

class bar(object):
    def __init__(self):
        self.c = C()
        self.c.addobserver(self.foo)
        print self.c._Observable__observers

    def foo(self, x):
        print 'callback', x  #never reached

b = bar()
b.c.set(3)

and here's the output:

ADDING observer <weakref at 0xaf1570; to 'instancemethod' at 0xa106c0 (foo)>
[<weakref at 0xaf1570; dead>]
observer was deleted (removing) <weakref at 0xaf1570; dead>

the main thing to note is that the print statement after the call to addobserver shows that the weakref is already dead.

Community
  • 1
  • 1
Adam Fraser
  • 6,255
  • 10
  • 42
  • 54
  • 1
    Do you realize that `__observers` is a class variable and thus shared across all instances of `Observable` (this includes subclasses)? Also, if you're using name mangling (imho you shouldn't, but that's a different topic), you shouldn't access the mangled names from outside. Specifically, you hardcode a class name and access something which you presumably try to keep private. –  Mar 22 '11 at 16:48
  • Ah, so I should get rid of the class var declaration and use `if "__observers" in self.__dict__` instead of the `if not self.__observers`. Right-o. Almost missed that. Also, I know the private var should be kept private, I was just printing it for demonstration purposes. This whole name-mangling thing is new to me though, so thanks for putting a name to it (no pun intended) so I could google it... I wasn't sure why __observers was getting renamed to _Observable__observers. I'll probably call it _observers instead. – Adam Fraser Mar 22 '11 at 17:06
  • delnan: it appears your comment about `__observers` being a class variable isn't entirely true. Initially it is, but when `addobserver` is called, `self.__observers = []` initializes the instance variable. The class variable is still accessible (and still `None`), but only reachable by `Observable._Observable__observers`. – Adam Fraser Mar 22 '11 at 20:20
  • delnan: it appears your comment about `__observers` being a class variable isn't entirely true. Initially it is, but when `addobserver` is called, `self.__observers = []` initializes the instance variable. The class variable is still accessible, but only by `Observable._Observable__observers`. – Adam Fraser Mar 22 '11 at 20:22

2 Answers2

4

Whenever you do reference an object method, there's a bit of magic that happens, and it's that magic that's getting in your way.

Specifically, Python looks up the method on the object's class, then combines it with the object itself to create a kind of callable called a bound method. Every time e.g. the expression self.foo is evaluated, a new bound method instance is created. If you immediately take a weakref to that, then there are no other references to the bound method (even though both the object and the class's method still have live refs) and the weakref dies.

See this snippet on ActiveState for a workaround.

Walter Mundt
  • 24,753
  • 5
  • 53
  • 61
3

Each time you access a method of an instance, obj.m, a wrapper (called "bound method" is generated) that's callable an adds self (obj) as first argument when called. This is a neat solution for passing self "implicitly" and allows passing instance methods in the first place. But it also means that each time you type obj.m, a new (very lightweight) object is created, and unless you keep a (non-weak) reference to it around, it will be GC'd, because nobody will keep it alive for you.