2012-01-06 39 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()