TypeError: main() takes exactly 1 argument (0 given)

TypeError: main() takes exactly 1 argument (0 given)

我正在尝试在 Python 2.7.11 和 运行 中的 class 文件中创建一个 main(),但是 Python 声称我需要传递 main() 一个参数。

def main(self):
    howManyBadCrops = BadCropsDetector() # My class
    # a bunch of stuff goes here that runs the module....

if __name__ == "__main__":
    main()

为什么会这样?这是我的终端输出:

Traceback (most recent call last):
  File "badCropsDetector.py", line 11, in <module>
    class BadCropsDetector:
  File "badCropsDetector.py", line 66, in BadCropDetector
    main()
TypeError: main() takes exactly 1 argument (0 given)

在此上下文中,您不需要 main 的函数定义中的 self 参数。这是因为 main 显然是一个模块级函数,您只需要在编写包含在 class 中的函数时指定 self (即 方法).

只需将其从定义中删除:

def main():