473,320 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 1514
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: HankC | last post by:
Greetings: I'm been programming Delphi for a while and am trying to get into the Python mindset. In Delphi, you can have a component class that has properties, methods, and events. Properties...
4
by: Dirk Thierbach | last post by:
I am writing an Python 2.3 extension in C, and I would like to create events (Tk-style) from an external source. I use a library that already parses the events into a C structure. This library can...
1
by: tim | last post by:
Someone using Python Midi Package from http://www.mxm.dk/products/public/ lately? I want to do the following : write some note events in a midi file then after doing that, put some controllers...
9
by: redefined.horizons | last post by:
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...
2
by: Oliver Nelson | last post by:
I have MapPoint working in Python, and I'm trying to cancel events on the map, but I can't seem to make that happen. I'm responding to the events successfully in my panel object. My code is like...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.