2

I am creating a stoppable thread class :

import threading

class StoppableThread(threading.Thread):
    """Thread class with a stop() method. The thread itself has to check
    regularly for the stopped() condition."""

    def __init__(self):
        super(StoppableThread, self).__init__()
        self._stop = threading.Event()

    def stop(self):
        self._stop.set()

    def stopped(self):
        return self._stop.isSet()

However when I create the object like so :

I receive this error:

Traceback (most recent call last):
  File "./myFile.py", line 81, in <module>
    aObject = StoppableThread(target, args=("foo", "bar",)))
TypeError: __init__() got an unexpected keyword argument 'args'

Thank you in advance.

2 Answers2

3

You have overridden init method and you don't have "args" argument there.

You have to do something like:

 def __init__(self, group=None, target=None, name=None, args=(), kwargs={}):
        super(StoppableThread, self).__init__(group, target, name, args, kwargs)
        self._stop = threading.Event()
Ashalynd
  • 12,363
  • 2
  • 34
  • 37
1

Your __init__ does not take any arguments. You want your __init__ function to accept any positional arguments and any keyword arguments which Thread.__init__ accepts. You want to pass all of them to the base implmentation:

def __init__(self, *args, **kwargs):
    super(StoppableThread, self).__init__(*args, **kwargs)
    # ...

See also: *args and **kwargs?

Community
  • 1
  • 1
zvone
  • 18,045
  • 3
  • 49
  • 77