Python和线程:实例没有调用方法
我在Python中扩展线程类时遇到问题。这是我的简单代码:Python和线程:实例没有调用方法,python,multithreading,Python,Multithreading,我在Python中扩展线程类时遇到问题。这是我的简单代码: import threading class position: def __init__(self,id): self.id = id class foo(threading.Thread): def __init__(self): self.start = position(0) threading.Thread.__init__(self) def
import threading
class position:
def __init__(self,id):
self.id = id
class foo(threading.Thread):
def __init__(self):
self.start = position(0)
threading.Thread.__init__(self)
def run(self):
pass
if __name__ == '__main__':
f = foo()
f.start()
显示的错误是:
Traceback (most recent call last):
File "foo.py", line 19, in <module>
f.start()
AttributeError: position instance has no __call__ method
回溯(最近一次呼叫最后一次):
文件“foo.py”,第19行,在
f、 开始()
AttributeError:position实例没有调用方法
错误在哪里?我花了3个小时寻找解决方案,但找不到。
我在工作中多次扩展了Thread类,但这次它不起作用。您已经用
位置
实例覆盖了开始
方法。以不同的方式命名您的位置
属性
例如
您已经用
位置
实例覆盖了开始
方法。以不同的方式命名您的位置
属性
例如
您已经使用构造函数中的
start
字段隐藏了线程.start
方法
重命名该字段(例如,将其重命名为startedAt),或者如果坚持将其命名为
start
,则使用foo.start(f)
在构造函数中隐藏了Thread.start
方法和start
字段
重命名该字段(例如,将其重命名为startedAt),或者如果您坚持将其命名为
start
,请使用foo.start(f)
是否可以“重做”源代码?我不明白你的意思。你能“重做”源代码吗?我不明白你的意思。
import threading
class position:
def __init__(self,id):
self.id = id
class foo(threading.Thread):
def __init__(self):
self.start_position = position(0) # self.start is now unharmed
threading.Thread.__init__(self)
def run(self):
pass
if __name__ == '__main__':
f = foo()
f.start()