Design thought for callbacks

Cem Karan cfkaran2 at gmail.com
Wed Feb 25 07:50:02 EST 2015


On Feb 24, 2015, at 4:19 PM, Gregory Ewing <greg.ewing at canterbury.ac.nz> wrote:

> random832 at fastmail.us wrote:
>> On Tue, Feb 24, 2015, at 00:20, Gregory Ewing wrote:
>>> This is why I suggested registering a listener object
>>> plus a method name instead of a callback. It avoids that
>>> reference cycle, because there is no long-lived callback
>>> object keeping a reference to the listener.
>> How does that help? Everywhere you would have had a reference to the
>> "callback object", you now have a reference to the listener object.
> 
> The point is that the library can keep a weak reference
> to the listener object, whereas it can't reliably keep
> a weak reference to a bound method.

I think I see what you're talking about now.  Does WeakMethod (https://docs.python.org/3/library/weakref.html#weakref.WeakMethod) solve this problem?  Note that I can force my users to use the latest stable version of python at all times, so WeakMethod IS available to me.

Thanks,
Cem Karan


More information about the Python-list mailing list