Events in Python?

nikie n.estner at gmx.de
Wed Apr 26 13:07:12 EDT 2006


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.




More information about the Python-list mailing list