Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; '"__main__":': 0.07; '__name__': 0.07; 'msg': 0.07; 'subject:code': 0.07; 'val': 0.07; 'subject:help': 0.07; '"in': 0.09; 'stackless': 0.09; 'python': 0.11; 'def': 0.14; 'output': 0.15; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'invokes': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'program?': 0.16; 'ravi': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'skip:{ 30': 0.16; 'subclasses,': 0.16; 'wrote:': 0.16; 'pass': 0.22; 'import': 0.24; 'header:In- Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'skip:_ 20': 0.26; 'switch': 0.27; 'skip:e 30': 0.27; 'function': 0.30; 'print': 0.31; 'skip:s 30': 0.31; 'skip:_ 10': 0.32; 'received:84': 0.32; 'class': 0.33; 'skip:_ 30': 0.33; 'to:addr:python-list': 0.35; 'instance': 0.35; 'skip:o 20': 0.35; 'hi,': 0.37; 'subject:: ': 0.37; 'skip:p 20': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'skip:e 20': 0.39; 'subject:the': 0.40; 'why': 0.40; '__call__': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=OoyysHLt c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=QrohdLjRRo4A:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=CKV5Z0YFK73q__RG9QQA:9 a=XVXjXf7Io3ue_Fbc:21 a=c0ybROdDUlCCMjyF:21 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett@:2500 Date: Thu, 18 Jun 2015 13:14:46 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: help in understanding the stackless code References: In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 87 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1434629697 news.xs4all.nl 2956 [2001:888:2000:d::a6]:50881 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:92824 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?