in c# for example i can write a delegate and an event in this way...
public delegate SomethingChangedHandler(string message);
public event SomethingChangedHandler SomethingChanged;
and later in the code fire this event in this way...
if(SomethingChanged != null)
and the subscription of this event of other objects can be easy as
eventGeneratorObject.SomethingChanged += new
and even the handlig of the event is aesy...
void aFunctionto_takecareof_it(string msg)
....now the question is.. how can i do the same using Python? Every help is
Python has no built-in way of doing this. You may consider writing
your own class if you like this pattern (I personally do):
self.subscribers = set()
def __iadd__(self, subscriber):
def __isub__(self, subscriber):
def __call__(self, *args, **kwargs):
for subscriber in self.subscribers:
print "HandleFoo:", strng
OnFoo = Event()
OnFoo += HandleFoo
> Hi there,
> Python has no built-in way of doing this. You may consider writing
> your own class if you like this pattern (I personally do):
> class Event(object):
> def __init__(self):
> self.subscribers = set()
> def __isub__(self, subscriber):
> return self
A slight typo there. For sets s.pop() returns an arbitrary element
and s.remove(x) or s.discard(x) removes the element x from the set s.
For the OP: remove raises an exception if x is not in the set,
discard does not.
[ a...@iki.fi <*> Antti Rasinen ]
"The music of rebellion makes you wanna rage
But it's made by millionaires who are nearly twice your age"
Txs so much.
I think the pubsub module in wxPython does what you are referring to.
Of course, usually you would need to be writing a GUI if you used it.
Here's some links on it:
They also mention it in their wiki / cookbook. Unfortunately, the
wxPython website seems screwed up today. I've never seen it behave
like this. But when it's back up, I'd highly recommend checking it
out. Or just browse Google's cached copies...