2012-01-06 58 views
-1

我下面举个例子扭曲loopingcall访问类瓦尔

class Intake: 
    def __init__(self): 

     # 
     # aggregate dict to store all the counters 
     # 
     self.counters = {} 

     # 
     # start a looping call to run reach minute 
     # 
     self.lc = task.LoopingCall(self.aggregate, self.counters) 
     self.lc.start(60) 


    def aggregate(self, counters): 
     print counters 

使作品就好了..但在我的聚合函数,我需要清除出self.counters字典。我有问题,这样做..

我要像做

def aggregate(self, counters): 
     print counters 

     self.counters = {} 

如果我在那个函数引用self.counters我得到

exceptions.AttributeError: Intake instance has no attribute 'counters' 
+0

...那么问题是什么?这不行吗? – kindall 2012-01-06 21:29:44

+0

我编辑了问题,在底部添加错误 – Mike 2012-01-06 21:39:35

回答

3

这是一个好主意,包括你的问题的一个可运行的例子,如果我尝试你描述它的工作正常。

from twisted.internet import task 

class Intake: 
    def __init__(self): 

     # 
     # aggregate dict to store all the counters 
     # 
     self.counters = {} 
     self.count = 0 
     # 
     # start a looping call to run reach minute 
     # 
     self.lc = task.LoopingCall(self.aggregate, self.counters) 
     self.lc.start(1) 


    def aggregate(self, counters): 
     print '%d, %r, %r' % (self.count, counters, self.counters) 
     self.count += 1 
     self.counters = {} 

if __name__ == "__main__": 
    from twisted.internet import reactor 
    r = Intake() 
    reactor.run()