Design thought for callbacks

Chris Angelico rosuav at gmail.com
Sun Feb 22 09:01:06 EST 2015


On Mon, Feb 23, 2015 at 12:45 AM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
>> No no no. It's the other way around. _Something_ has to be doing those
>> callbacks, and it's that _something_ that should be keeping them
>> alive. The fact that it's a registered callback should itself *be* a
>> reference (and not a weak reference), and should keep it alive.
>
> That's much more reasonable than what you said earlier:
>
>     it seems wrong to have to stash a thing in a bucket in order
>     to keep its callbacks alive. I expect the callbacks themselves to
>     keep it alive.
>
>
> So yes. If I bind a callback to a button, say, or a listener, then the
> button (or listener) keeps the callback alive, *not* the callback keeping
> the button or listener alive.

I meant the same thing, but my terminology was poor. Yes, that's
correct; it's not any sort of magic about it being a callback, but
more that the one you register it with becomes the owner of something.
Hence, no weak references.

ChrisA



More information about the Python-list mailing list