1

I have a problem extending Thread class in Python. This is my simple code:

import threading

class position:

    def __init__(self,id):
        self.id = id

class foo(threading.Thread):

    def __init__(self):
        self.start = position(0)
        threading.Thread.__init__(self)

    def run(self):
        pass

if __name__ == '__main__':
    f = foo()
    f.start()

The shown error is:

Traceback (most recent call last):
  File "foo.py", line 19, in <module>
    f.start()
AttributeError: position instance has no __call__ method

Where is the error? i have spended 3 hours searching for a solution, but i can't find one. I have extended Thread class many time during my work, but this time it won't work.

4

2 に答える 2

6

You've overwritten the start method with your position instance. Name your position property differently.

E.g.

import threading

class position:

    def __init__(self,id):
        self.id = id

class foo(threading.Thread):

    def __init__(self):
        self.start_position = position(0)   # self.start is now unharmed
        threading.Thread.__init__(self)

    def run(self):
        pass

if __name__ == '__main__':
    f = foo()
    f.start()
于 2012-12-29T14:45:37.403 に答える
2

You have hidden the Thread.start method with your start field in the constructor.

Either rename the field (to startedAt, for instance), or if you insist on leaving it called start, use foo.start(f)

于 2012-12-29T14:46:47.280 に答える