Python 类型错误:worker() 采用 0 个位置参数,但给出了 1 个

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18884782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:10:09  来源:igfitidea点击:

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

pythonpython-3.x

提问by StatsViaCsh

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

我正在尝试实现一个子类并抛出错误:

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

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

回答by TerryA

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

您忘记将其self作为参数添加到worker()类中的函数中KeyStatisticCollection

回答by Atra Azami

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

您的worker方法需要“self”作为参数,因为它是类方法而不是函数。添加它应该使它工作正常。

回答by LeilaHC

If the method doesn't require selfas an argument, you can use the @staticmethoddecorator to avoid the error:

如果该方法不需要self作为参数,则可以使用@staticmethod装饰器来避免错误:

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

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

回答by Nitesh Sharma

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

回答by makkasi

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

检查是否从名为 method_a() 的方法调用具有相同名称的方法 method_a(with_params) 导致递归

回答by scooter

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

此错误的另一个用例是在类定义中导入函数时。这使得后续函数调用成为类对象的一部分。在这种情况下,您可以在库导入函数上使用 @staticmethod 或直接对该函数进行静态路径调用。见下面的例子

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

在这个例子中“self.bar()”会抛出一个TypeError,但是可以通过两种方式修复

# 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:

选项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:

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

回答by Stryker

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 automaticallypasses self as the first argument.

当您在类上调用方法时(例如work()在这种情况下),Python 会自动将 self 作为第一个参数传递。

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

让我们再读一遍: 当你在一个类上调用一个方法时(比如work()在这种情况下),Python 会自动将 self 作为第一个参数传递

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 selfargument is still being passed automaticallywhen the method is called. So you better fix this and put that selfkeyword back in.

所以这里 Python 是说,嘿,我可以看到它work()接受 0 个位置参数(因为括号内没有任何内容),但是您知道在self调用方法时仍然会自动传递该参数 。所以你最好解决这个问题,然后把那个self关键字放回去。

Adding selfshould resolve the problem. work(self)

添加self应该可以解决问题。work(self)

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