Skip to main content
 首页 » 编程设计

Python 和线程 : instance has no __call__ method

2025年05月04日15三少

我在 Python 中扩展 Thread 类时遇到问题。这是我的简单代码:

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 

错在哪里?我花了 3 个小时寻找解决方案,但找不到。 工作中多次扩展Thread类,但这次不行。

请您参考如下方法:

您已经用position 实例覆盖了start 方法。以不同的方式命名您的 position 属性。

例如

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()