导致“必须以实例作为第一个参数调用未绑定方法__init __()"的原因是什么?从这个Python代码? [英] What is causing "unbound method __init__() must be called with instance as first argument" from this Python code?
本文介绍了导致“必须以实例作为第一个参数调用未绑定方法__init __()"的原因是什么?从这个Python代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我上了这个课:
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)
并使用以下脚本对其进行调用:
and am calling it with this script:
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
and get this error and I can't figure out why: unbound method __init__() must be called with instance as first argument
推荐答案
您正在做
Thread.__init__()
使用:
Thread.__init__(self)
或者,而是使用super()
这篇关于导致“必须以实例作为第一个参数调用未绑定方法__init __()"的原因是什么?从这个Python代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文