What is causing "unbound method __init__() must be called with instance as first argument" from this Python code?

Leonidas picture Leonidas · Oct 23, 2009 · Viewed 28.8k times · Source

I have this class:

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

and get this error and I can't figure out why: unbound method __init__() must be called with instance as first argument

Answer

truppo picture truppo · Oct 23, 2009

You are doing:

Thread.__init__() 

Use:

Thread.__init__(self) 

Or, rather, use super()