gpt4 book ai didi

python - 为什么 super(Thread, self).__init__() 不能用于 threading.Thread 子类?

转载 作者:太空狗 更新时间:2023-10-29 17:00:05 35 4
gpt4 key购买 nike

我所知道的 Python 中的每个对象都可以通过调用来处理其基类初始化:

super(BaseClass, self).__init__()

threading.Thread 的子类似乎不是这种情况,因为如果我在 SubClass.__init__() 中尝试这个,我得到:

RuntimeError: thread.__init__() not called

是什么导致了这个错误?我查看了 threading.Thread 的源代码,看起来 __init__ 方法应该设置 Thread.__initialized = True。我看到所有示例都使用以下 __init__:

class YourThread(threading.Thread):
def __init__(self, *args):
threading.Thread.__init__(self)
# whatev else

但为什么呢?

最佳答案

这很好用:

>>> class MyThread(threading.Thread):
... def __init__(self):
... super(MyThread, self).__init__()

我认为您的代码的错误在于您将 base 类而不是 current 类传递给 super —— 即你正在调用 super(threading.Thread, ...,这是错误的。很难说,因为你没有显示失败的代码,但这就是我从你的语言中间接推断出来的正在使用!-)

关于python - 为什么 super(Thread, self).__init__() 不能用于 threading.Thread 子类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2197563/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com