Events in Python?

Ben C spamspam at spam.eggs
Thu Apr 27 04:03:42 EDT 2006


On 2006-04-26, nikie <n.estner at gmx.de> wrote:
> redefined.horizons at gmail.com wrote:
>
>> Here is another non-pythonic question from the Java Developer. (I beg
>> for forgiveness...)
>>
>> Does Python have a mechanism for events/event-driven programming?
>>
>> I'm not necessarily talking about just GUIs either, I'm interested in
>> using events for other parts of an application as well.
>>
>> If there isn't some sort of event mechanism built into Python, where
>> might I find some information about implementing one?
>
> Maybe I got your question wrong, but why not simply use something like:
>
> class Sender:
>     def __init__(self, event):
>         self.event = event
>
>     def raiseEvent(self):
>         self.event("Event")
>
> class Receiver:
>     def receiveEvent(self, msg):
>         print "Received %r" % msg
>
> r = Receiver()
> s = Sender(r.receiveEvent)
> s.raiseEvent()
>
> You can pass around functions and bound methods, I always found that's
> often a really good substitute for full-blown event mechanisms.

Actually I'd say full-blown event mechanisms are a poor substitute for
passing around bound-methods :)



More information about the Python-list mailing list