Design thought for callbacks

Gregory Ewing greg.ewing at canterbury.ac.nz
Sun Feb 22 05:15:30 EST 2015


Frank Millman wrote:
> "In order to inform users that certain bits of state have changed, I require 
> them to register a callback with my code."
> 
> This sounds to me like a pub/sub scenario. When a 'listener' object comes 
> into existence it is passed a reference to a 'controller' object that holds 
> state. It wants to be informed when the state changes, so it registers a 
> callback function with the controller.

Perhaps instead of registering a callback function, you
should be registering the listener object together with
a method name.

You can then keep a weak reference to the listener object,
since if it is no longer referenced elsewhere, it presumably
no longer needs to be notified of anything.

-- 
Greg



More information about the Python-list mailing list