471,337 Members | 1,194 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

Events: The Python Way

Hi,
i'm a .net programmer and i'm learnig python, so this question can be very
stupid or easy for python programmers. I've a doubt about events.... here is
what:

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)
{
SomethingChanged("Nothing important");
}

and the subscription of this event of other objects can be easy as

eventGeneratorObject.SomethingChanged += new
SomethingChangedHandler(aFunctionto_takecareof_it) ;

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
appreciated

Regards,
Gianmaria

Jul 28 '07 #1
4 1423
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 __iadd__(self, subscriber):
self.subscribers.add(subscriber)
return self

def __isub__(self, subscriber):
self.subscribers.pop(subscriber)
return self

def __call__(self, *args, **kwargs):
for subscriber in self.subscribers:
subscriber(*args, **kwargs)
def HandleFoo(strng):
print "HandleFoo:", strng

OnFoo = Event()
OnFoo += HandleFoo

OnFoo("Test.")
On 29/07/07, Gianmaria <gi*******@hotmail.comwrote:
Hi,
i'm a .net programmer and i'm learnig python, so this question can be very
stupid or easy for python programmers. I've a doubt about events.... here is
what:

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)
{
SomethingChanged("Nothing important");
}

and the subscription of this event of other objects can be easy as

eventGeneratorObject.SomethingChanged += new
SomethingChangedHandler(aFunctionto_takecareof_it) ;

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
appreciated

Regards,
Gianmaria

--
http://mail.python.org/mailman/listinfo/python-list
Jul 28 '07 #2

On 2007-07-29, at 02:34, David Wilson wrote:
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):
self.subscribers.pop(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.

--
[ ar*@iki.fi <*Antti Rasinen ]

"The music of rebellion makes you wanna rage
But it's made by millionaires who are nearly twice your age"
Jul 29 '07 #3
"David Wilson" <dw@botanicus.netha scritto nel messaggio
news:ma***************************************@pyt hon.org...
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 __iadd__(self, subscriber):
self.subscribers.add(subscriber)
return self

def __isub__(self, subscriber):
self.subscribers.pop(subscriber)
return self

def __call__(self, *args, **kwargs):
for subscriber in self.subscribers:
subscriber(*args, **kwargs)
def HandleFoo(strng):
print "HandleFoo:", strng

OnFoo = Event()
OnFoo += HandleFoo

OnFoo("Test.")
On 29/07/07, Gianmaria <gi*******@hotmail.comwrote:
>Hi,
i'm a .net programmer and i'm learnig python, so this question can be
very
stupid or easy for python programmers. I've a doubt about events.... here
is
what:

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)
{
SomethingChanged("Nothing important");
}

and the subscription of this event of other objects can be easy as

eventGeneratorObject.SomethingChanged += new
SomethingChangedHandler(aFunctionto_takecareof_it );

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
appreciated

Regards,
Gianmaria

--
http://mail.python.org/mailman/listinfo/python-list
Txs so much.
Gianmaria

Jul 29 '07 #4
On Jul 29, 3:14 pm, "Gianmaria" <gianma...@hotmail.comwrote:
"David Wilson" <d...@botanicus.netha scritto nel messaggionews:ma********************************** *****@python.org...
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 __iadd__(self, subscriber):
self.subscribers.add(subscriber)
return self
def __isub__(self, subscriber):
self.subscribers.pop(subscriber)
return self
def __call__(self, *args, **kwargs):
for subscriber in self.subscribers:
subscriber(*args, **kwargs)
def HandleFoo(strng):
print "HandleFoo:", strng
OnFoo = Event()
OnFoo += HandleFoo
OnFoo("Test.")
On 29/07/07, Gianmaria <gianma...@hotmail.comwrote:
Hi,
i'm a .net programmer and i'm learnig python, so this question can be
very
stupid or easy for python programmers. I've a doubt about events.... here
is
what:
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)
{
SomethingChanged("Nothing important");
}
and the subscription of this event of other objects can be easy as
eventGeneratorObject.SomethingChanged += new
SomethingChangedHandler(aFunctionto_takecareof_it) ;
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
appreciated
Regards,
Gianmaria
--
http://mail.python.org/mailman/listinfo/python-list

Txs so much.
Gianmaria
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:

http://www.wxpython.org/docs/api/wx....ub-module.html
http://www.wxpython.org/docs/api/wx....ass-class.html

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

Mike

Jul 30 '07 #5

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
9 posts views Thread by redefined.horizons | last post: by
2 posts views Thread by Oliver Nelson | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.