Design thought for callbacks

Devin Jeanpierre jeanpierreda at gmail.com
Sat Feb 21 09:24:36 EST 2015


On Fri, Feb 20, 2015 at 9:42 PM, Chris Angelico <rosuav at gmail.com> wrote:
> No, it's not. I would advise using strong references - if the callback
> is a closure, for instance, you need to hang onto it, because there
> are unlikely to be any other references to it. If I register a
> callback with you, I expect it to be called; I expect, in fact, that
> that *will* keep my object alive.

For that matter, if the callback is a method, you need to hang onto
it, because method wrappers are generated on demand, so the method
would be removed from the valid callbacks instantly.

Weak references for callbacks are broken.

-- Devin



More information about the Python-list mailing list