关于python:TypeError:worker()接受0位置参数,但给出了1

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

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

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

1
2
3
4
5
6
7
class KeyStatisticCollection(DataDownloadUtilities.DataDownloadCollection):
    def GenerateAddressStrings(self):
        pass    
    def worker():
        pass
    def DownloadProc(self):
        pass


您的worker方法需要'self'作为参数,因为它是类方法而不是函数。补充说一下,这会使它工作得很好。


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

1
2
3
4
5
6
7
8
9
10
11
class KeyStatisticCollection(DataDownloadUtilities.DataDownloadCollection):

    def GenerateAddressStrings(self):
        pass    

    @staticmethod
    def worker():
        pass

    def DownloadProc(self):
        pass

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


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


1
2
3
4
5
6
7
class KeyStatisticCollection(DataDownloadUtilities.DataDownloadCollection):
def GenerateAddressStrings(self):
    pass    
def worker(self):
    pass
def DownloadProc(self):
    pass