[python] TypeError: worker() takes 0 positional arguments but 1 was given

I'm trying to implement a subclass and it throws the error:

TypeError: worker() takes 0 positional arguments but 1 was given

class KeyStatisticCollection(DataDownloadUtilities.DataDownloadCollection):
    def GenerateAddressStrings(self):
        pass    
    def worker():
        pass
    def DownloadProc(self):
        pass

This question is related to python python-3.x

The answer is


Your worker method needs 'self' as a parameter, since it is a class method and not a function. Adding that should make it work fine.


another use case for this error is when you import functions within the class definition. this makes the subsequent function calls a part of the class object. In this case you can use @staticmethod on the library import function or make a static path call directly to the function. see example below

In this example "self.bar()" will throw a TypeError, but it can be fixed in two ways

# in lib.py
def bar():
  print('something to do')

# in foo.py
class foo():
  from .lib import bar

  def __init__(self):
    self.bar()

Option 1:

# in lib.py
def bar():
  print('something to do')

# in foo.py
class foo():
  from .lib import bar

  def __init__(self):
    lib.bar()

Option 2:

# in lib.py:
@staticmethod
def bar():
  print('something to do')

# in foo.py
class foo():
  from .lib import bar

  def __init__(self):
    self.bar()

When doing Flask Basic auth I got this error and then I realized I had wrapped_view(**kwargs) and it worked after changing it to wrapped_view(*args, **kwargs).


I get this error whenever I mistakenly create a Python class using def instead of class:

def Foo():
    def __init__(self, x):
        self.x = x
# python thinks we're calling a function Foo which takes 0 args   
a = Foo(x) 

TypeError: Foo() takes 0 positional arguments but 1 was given

Oops!


If the method doesn't require self as an argument, you can use the @staticmethod decorator to avoid the error:

class KeyStatisticCollection(DataDownloadUtilities.DataDownloadCollection):

    def GenerateAddressStrings(self):
        pass    

    @staticmethod
    def worker():
        pass

    def DownloadProc(self):
        pass

See https://docs.python.org/3/library/functions.html#staticmethod


Check if from method with name method_a() you call method with the same name method_a(with_params) causing recursion


class KeyStatisticCollection(DataDownloadUtilities.DataDownloadCollection):
def GenerateAddressStrings(self):
    pass    
def worker(self):
    pass
def DownloadProc(self):
    pass

You forgot to add self as a parameter to the function worker() in the class KeyStatisticCollection.


This can be confusing especially when you are not passing any argument to the method. So what gives?

When you call a method on a class (such as work() in this case), Python automatically passes self as the first argument.

Lets read that one more time: When you call a method on a class (such as work() in this case), Python automatically passes self as the first argument

So here Python is saying, hey I can see that work() takes 0 positional arguments (because you have nothing inside the parenthesis) but you know that the self argument is still being passed automatically when the method is called. So you better fix this and put that self keyword back in.

Adding self should resolve the problem. work(self)

class KeyStatisticCollection(DataDownloadUtilities.DataDownloadCollection):
def GenerateAddressStrings(self):
    pass    
def worker(self):
    pass
def DownloadProc(self):
    pass