Events in Python?

R

redefined.horizons

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
 
D

Diez B. Roggisch

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
 
A

Avizoa

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
 
J

John Hunter

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
 
N

nikie

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.
 
R

redefined.horizons

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
 
P

Philippe Martin

Besides the other anwsers, you might want to check the signal module.

Regards,

Philippe
 
B

Ben C

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 :)
 
B

Ben Sizer

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,161
Latest member
GertrudeMa
Top