TypeError: worker()接受0个位置参数,但是给出了1个

我试图实现一个子类,它抛出了一个错误:

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
284570 次浏览

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

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.

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

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

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

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()

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

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!

just pass self keyword in def worker(): function

class KeyStatisticCollection(DataDownloadUtilities.DataDownloadCollection):
def GenerateAddressStrings(self):
pass
def worker(self):
pass
def DownloadProc(self):
pass
 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