Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #92847
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2015-06-18 12:41 -0700 |
| References | <f0d40f43-36ee-4c41-9892-9b5ee524fea3@googlegroups.com> <mailman.596.1434629697.13271.python-list@python.org> |
| Message-ID | <8e34cc5a-9577-4f63-b08a-fe70aa406621@googlegroups.com> (permalink) |
| Subject | Re: help in understanding the stackless code |
| From | ravi <temp.sha@gmail.com> |
yes It has instance of both Reporter and Switch.
moreover I could not get why instance "reporter" is passed to class Switch
as a parameter ?
> > reporter = Reporter()
> > switch = Switch(0,reporter)
> > switch(1)
thanks
On Thursday, June 18, 2015 at 5:45:08 PM UTC+5:30, MRAB wrote:
> On 2015-06-18 08:41, ravi wrote:
> > hi,
> > I am new to python and need to know why the calling of switch(1) invokes the function "listen" twice in the below program?
> >
> >
> >
> > import stackless
> >
> > class EventHandler:
> > def __init__(self,*outputs):
> > if outputs==None:
> > self.outputs=[]
> > else:
> > self.outputs=list(outputs)
> >
> > self.channel = stackless.channel()
> > stackless.tasklet(self.listen)()
> >
> > def listen(self):
> > print "in listen()..."
> > while 1:
> > val = self.channel.receive()
> > self.processMessage(val)
> > for output in self.outputs:
> > self.notify(output)
> >
> > def processMessage(self,val):
> > pass
> >
> > def notify(self,output):
> > pass
> >
> > def registerOutput(self,output):
> > print "in registerOutput()..."
> > self.outputs.append(output)
> >
> > def __call__(self,val):
> > print "in __call__ ..."
> > self.channel.send(val)
> >
> > class Switch(EventHandler):
> > def __init__(self,initialState=0,*outputs):
> > EventHandler.__init__(self,*outputs)
> > self.state = initialState
> >
> > def processMessage(self,val):
> > print "in processMessage() of Switch..."
> > self.state = val
> >
> > def notify(self,output):
> > print "in notify() of switch..."
> > output((self,self.state))
> >
> > class Reporter(EventHandler):
> > def __init__(self,msg="%(sender)s send message %(value)s"):
> > EventHandler.__init__(self)
> > self.msg = msg
> >
> > def processMessage(self,msg):
> > print "in processMessage() of Reporter..."
> > sender,value=msg
> > print self.msg % {'sender':sender,'value':value}
> >
> >
> > if __name__ == "__main__":
> > reporter = Reporter()
> > switch = Switch(0,reporter)
> > switch(1)
> >
> >
> >
> >
> > output:
> > =========
> >
> > in __call__ ...
> > in listen()...
> > in listen()...
> > in processMessage() of Switch...
> > in notify() of switch...
> > in __call__ ...
> > in processMessage() of Reporter...
> > <__main__.Switch instance at 0x8d822cc> send message 1
> >
> Is it because EventHandler has 2 subclasses, namely Switch and
> Reporter, and you have an instance of each?
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
help in understanding the stackless code ravi <temp.sha@gmail.com> - 2015-06-18 00:41 -0700
Re: help in understanding the stackless code MRAB <python@mrabarnett.plus.com> - 2015-06-18 13:14 +0100
Re: help in understanding the stackless code ravi <temp.sha@gmail.com> - 2015-06-18 12:41 -0700
Re: help in understanding the stackless code Laura Creighton <lac@openend.se> - 2015-06-19 00:14 +0200
csiph-web