tags:

views:

840

answers:

3

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

+3  A: 

This is a frequently asked question at SO, but the answer, in brief, is that the way you call your superclass's constructor is like:

super(Timer,self).__init__()
Jonathan Feinberg
+6  A: 

You are doing:

Thread.__init__()

Use:

Thread.__init__(self)

Or, rather, use super()

truppo
That'd be `super(Thread, self).__init__()` -- but super has it's own problems too :/
THC4k
@THC4k: Super has no problems, multiple inheritance has problems. And if you use multiple inheritance then super is much much better than direct calls.
nikow
A: 

You just need to pass 'self' as an argument to 'Thread.init'. After that, it works on my machines.

tsellon