[wxPython-users] custom events vs call backs

Peter Hansen peter at engcorp.com
Mon Nov 27 05:27:33 PST 2006


Donn Ingle wrote:
> So, to get my ship off the bottom and sailing again, please tell me which is
> better and why?

While callbacks (especially those set up in your initializer) are a fine 
approach (certainly better that directly calling methods in your 
parent!), events are often a better approach.  It's something you should 
probably determine on a case-by-case basis, at least until you get a 
feel for it.

The theoretical reason events are better is that they decrease coupling. 
  (Callbacks do that pretty well, so that's why they're often appropriate.)

The practical reason events might be better is that they should make 
testing easier, partly because you can now instantiate your custom 
control stand-alone, without having to provide places for it to call 
back to.  (Given that providing a dummy callback is pretty trivial, this 
is also not a really strong reason to favour events.)

Another practical reason is that with CommandEvents (and if you call 
event.Skip() properly), multiple recipients can be set up for your 
event, whereas with the callback approach managing that would get clumsy.

-Peter




More information about the wxpython-users mailing list