If you simply need to do an Object.onSomething() probably you need to write a reactor over a object data model.
What I mean? Put every relevant objects into a container make this container available to everyone. This container will implement the method
def castEvent(self, eventName, args, kargs):
In this method you iterate the containing object looking for the method "does%s" % eventName.
something like:
for item in self.items:
f = getattr(item, "does%s" % eventName, lambda *x, **k : False)
if f evaluates True using the parameters passed to the function then you call the "do%s"%eventName with the same parameters:
for item in self.items:
f = getattr(item, "does%s" % eventName, lambda *x, **k : False)
if f(*args, **kargs):
f = getattr(item, "do%s" % eventName, lambda *x, **k : False)
f(*args, **kargs)
Every time you need to send an event - say Click - you simply need to:
container.castEvent("Click",[],{"x":10,"y":20})
every object interested in Clicks has to provide a
def doesClick(self, x, y):
that manage the click events returning False or
def doesClick(self, x, y):
[...]
def doClick(self, x, y):
The first returning whether (x,y) hits the object and the second doing the click routine.
You can implement the first in a generic superclass and let the clickable classes to extend.
This is a very basic implementation whit a lot of room for improvements.