TypeError: worker() takes 0 positional arguments but 1 was given
PythonPython 3.xPython Problem Overview
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
Python Solutions
Solution 1 - Python
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.
Solution 2 - Python
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
Solution 3 - Python
You forgot to add self
as a parameter to the function worker()
in the class KeyStatisticCollection
.
Solution 4 - Python
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
Solution 5 - Python
class KeyStatisticCollection(DataDownloadUtilities.DataDownloadCollection):
def GenerateAddressStrings(self):
pass
def worker(self):
pass
def DownloadProc(self):
pass
Solution 6 - Python
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!
Solution 7 - Python
Check if from method with name method_a() you call method with the same name method_a(with_params) causing recursion
Solution 8 - Python
just pass self keyword in def worker(): function
class KeyStatisticCollection(DataDownloadUtilities.DataDownloadCollection):
def GenerateAddressStrings(self):
pass
def worker(self):
pass
def DownloadProc(self):
pass
Solution 9 - Python
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()
Solution 10 - Python
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).
Solution 11 - Python
class KeyStatisticCollection():
def GenerateAddressStrings(self):
pass
def worker():
return blabla
def DownloadProc(self):
abc = self.GenerateAddressStrings()
#abc = GenerateAddressStrings()#error
blabla = worker()
#blabla = self.worker()#error
i think this is a better explaination about using self param