472,993 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,993 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 1503
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.