我有這個類:什麼是導致「unbound method __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)
和我有這個腳本調用它:
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
這將是超級(線程,自我).__ init __()' - 但超級也有它自己的問題:/ – 2009-10-23 18:35:27
@ THC4k:超級沒有問題,多重繼承有問題。如果你使用多重繼承,那麼super比直接調用要好得多。 – nikow 2009-10-24 09:03:18
super僅僅是一個災難的祕訣,特別是在多重繼承中,尤其是如果有任何需要重新加載的擴展。 – dashesy 2014-09-09 21:10:05