2011-03-04 274 views
0

請幫助我的代碼與線程。它是這樣對象沒有屬性

class MyThread(threading.Thread): # Create a class representing a thread of control 
     def __init__(self,target): 
      print 'thread created' 
      self.target = target 
      threading.Thread.__init__ (self) 
     def run (self): 
      print 'running thread ' 
      while True: 
       self.target() 
    # Define class to allow thread to be stopped over time 
     def __init__ (self, target): 
      super(MyThread, self).__init__() 
      self._stop = threading.Event() 
      print "thread stopped" 
     def stop (self): 
      self._stop.set() 
     def stopped (self): 
      return self._stop.isSet() 

然而,當我運行它,它抱怨該行self.target():「MyThread的」對象有沒有屬性「目標」

我如何解決此得到什麼?

回答

4

您有兩個init函數定義。第二個定義(不定義目標)覆蓋第一個定義。

0

嚴......你重載你自己的init方法,並且覆蓋不會分配self.target變量。