Design thought for callbacks

random832 at fastmail.us random832 at fastmail.us
Tue Feb 24 00:29:51 EST 2015


On Tue, Feb 24, 2015, at 00:20, Gregory Ewing wrote:
> Cem Karan wrote:
> > I tend to structure my code as a tree or DAG of objects.  The owner refers to
> > the owned object, but the owned object has no reference to its owner.  With
> > callbacks, you get cycles, where the owned owns the owner.
> 
> 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.
You're just shuffling deck chairs around: if B shouldn't reference A
because A owns B, then removing C from the B->C->A reference chain does
nothing to fix this.



More information about the Python-list mailing list