python - 是什么导致此 Python 代码出现 "unbound method __init__() must be called with instance as first argument"?

标签 python init

我有这门课:

from threading import Thread 
import time

class Timer(Thread): 
    def __init__(self, interval, function, *args, **kwargs): 
        Thread.__init__() 
        self.interval = interval 
        self.function = function 
        self.args = args 
        self.kwargs = kwargs 
        self.start()

    def run(self): 
        time.sleep(self.interval) 
        return self.function(*self.args, **self.kwargs) 

我用这个脚本调用它:

    import timer 
    def hello():
        print \"hello, world
    t = timer.Timer(1.0, hello)
    t.run()

得到这个错误,我不知道为什么:unbound method __init__() must be called with instance as first argument

最佳答案

你正在做:

Thread.__init__() 

使用:

Thread.__init__(self) 

或者,更确切地说,使用 super()

关于python - 是什么导致此 Python 代码出现 "unbound method __init__() must be called with instance as first argument"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1615148/

相关文章:

python - 元素不可点击(按钮被其他元素阻挡)

python - 将文本输出写入 S3 存储桶的最佳实践是什么?

Java带参数执行嵌入式Python

linux - 如何让我的 init.d 脚本改变用户

python - 从 CSV 文件中提取列 Python : ValueError: too many values to unpack (expected 4)

python - 根据一行中的值对 pandas 数据框的列进行排序

class - 类初始化中的可选参数

python 在将参数传递给 init 时引用类变量

git - 如何在 EC2 cfn-init 上克隆 CodeCommit 存储库?

java - 为什么Spring boot Filter init函数以不同的顺序执行pack为jar和war包