2011-03-04 345 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变量。