[PyQt] Re: Virtual methods and an extra reference

Kevin Watters kevinwatters at gmail.com
Thu Jun 19 15:07:23 BST 2008

Sundance <sundance <at> ierne.eu.org> writes:

> This class behaves like weakref.ref, only for callables.

We use a variant of this idea in our app for observable callbacks:

class bound_ref(object):
    def __init__(self, method, cb = None):
        self.object = weakref_ref(method.im_self, cb)
        self.func   = method.im_func
        self.cls    = method.im_class

    def __call__(self):
        obj = self.object()
        if obj is None:
            return None

        return new.instancemethod(self.func, obj, self.cls)

Obviously the usage of the above bound_ref is just for bound methods--but I
think using new.instancemethod is more correct than lazily grabbing the bound
method with getattr. The only issue remaining is to whip up a C implementation
:-)  This is important enough that I'll try to look into it today, unless Phil
raises serious objections about its feasibility :P

> Might it be possible to have it used by PyQt for virtual methods?
> Thanks,

More information about the PyQt mailing list