Python 对象没有属性
请帮助我的线程代码。就像Python 对象没有属性,python,multithreading,Python,Multithreading,请帮助我的线程代码。就像 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 )
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'对象没有属性'target'
我该怎么做 您定义了两个init函数。第二个定义(不定义目标)覆盖第一个定义。您定义了两个init函数。第二个定义(不定义目标)覆盖第一个定义。嗯。。。您正在重写自己的init方法,该重写不会分配self.target变量。嗯。。。您正在重写自己的init方法,该重写不会指定self.target变量