467,134 Members | 1,091 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,134 developers. It's quick & easy.

Events in Python?

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?

Thanks,

Scott Huey

Apr 26 '06 #1
  • viewed: 2376
Share:
9 Replies
re****************@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?


In python 2.5, generators accept parameters. That might be a good
starting-point for an event-system.

Diez
Apr 26 '06 #2
re****************@gmail.com schrieb:

Does Python have a mechanism for events/event-driven programming?


Probably this is something for you:

http://twistedmatrix.com/trac/

--
Servus, Gregor
http://www.gregor-horvath.com
Apr 26 '06 #3

re****************@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?

Thanks,

Scott Huey

Technically this is a question more of perception than capability.

All programming languages are event driven.

Calling a function is an event, for instance.

What I suspect you mean is an event management system, which is what OO
state machines are all about.

If you actually mean something akin in function to GUI event systems,
you're really talking about message passing. In the case of message
passing you just need to build a simple event manager class that echoes
a message to all subscribed listening objects. In python that message
can be a function, object, arguments, text, etc. This is easily done
without threading, in case you're worried about that. The function of
the event manager that is used for the purpose of initiating events can
simply call a particular method of all subscribing objects held in a
list.

Simple as can be :)

~Brendan Kohler

Apr 26 '06 #4
>>>>> "redefined" == redefined horizons <re****************@gmail.com> writes:

redefined> Here is another non-pythonic question from the Java
redefined> Developer. (I beg for forgiveness...)

redefined> Does Python have a mechanism for events/event-driven
redefined> programming?

The enthought traits package has built-in support for event handling,
among other things

http://code.enthought.com/traits/

Here is an example from the web page:

from enthought.traits import Delegate, HasTraits, Int, Str, Instance
from enthought.traits.ui import View, Item

class Parent(HasTraits):
first_name = Str('') # INITIALIZATION:
last_name = Str('') # 'first_name' and
# 'last_name' are
# initialized to ''
class Child(HasTraits):
age = Int

father = Instance(Parent) # VALIDATION: 'father' must
# be a Parent instance

first_name = Str('')

last_name = Delegate('father') # DELEGATION:
# 'last_name' is
# delegated to
# father's 'last_name'

def _age_changed(self, old, new): # NOTIFICATION:
# This method is
# called when 'age'
# changes
print 'Age changed from %s to %s ' % (old, new)

traits_view = View(Item(name='first_name'), # TRAITS UI: Define
Item(name='last_name', # the default window
style='readonly'), # layout
Item(name='age'),
Item(name='father'))
################################################## ##
# Make and manipulate objects from the classes above
################################################## ##

joe = Parent()
joe.last_name = 'Johnson'

# DELEGATION in action
moe = Child()
moe.father = joe
print "Moe's last name is %s" % (moe.last_name)

# NOTIFICATION in action
moe.age = 10

#VISUALIZATION: Display the UI
moe.configure_traits()

The DELEGATION and NOTIFICATION segments in the above example yield
the following command-line output:

Moe's last name is Johnson
Age changed from 0 to 10

Apr 26 '06 #5
re****************@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.

Apr 26 '06 #6
Thank you for all of the responses. I will check out the Traits link.
It looked very interesting.

It seems like Python doesn't have a "standard" implementation of an
event or messaging system. That is really what I was curious about. I
wanted to check before I implemented something of my own.

Thanks again for the help.

Scott Huey

Apr 26 '06 #7
Besides the other anwsers, you might want to check the signal module.

Regards,

Philippe

re****************@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?

Thanks,

Scott Huey


Apr 26 '06 #8
re****************@gmail.com wrote:
It seems like Python doesn't have a "standard" implementation of an
event or messaging system. That is really what I was curious about. I
wanted to check before I implemented something of my own.


What are you comparing it to? Does Java have standard event or
messaging systems? I thought there were only such systems as part of
the GUI libraries. Perhaps you're referring to the Observer interface?
Sometimes a solution that is necessary in Java would be an
overcomplication in Python, and full-blown Observers is probably one
such example.

--
Ben Sizer

Apr 27 '06 #9
On 2006-04-26, nikie <n.******@gmx.de> wrote:
re****************@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 :)
Apr 27 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by HankC | last post: by
4 posts views Thread by Dirk Thierbach | last post: by
5 posts views Thread by factory | last post: by
reply views Thread by Mark Asbach | last post: by
2 posts views Thread by Dan | last post: by
4 posts views Thread by Stephan Kuhagen | last post: by
4 posts views Thread by Gianmaria | last post: by
2 posts views Thread by Oliver Nelson | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.