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

僤鯓⒐⒋嵵緔 提交于 2020-01-25 20:08:30

问题


I'm trying to create a main() in a class file in Python 2.7.11 and run it, but Python is claiming I need to pass main() an argument.

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

if __name__ == "__main__":
    main()

Why is this happening? Here is my terminal output:

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)

回答1:


In this context, you don't need the self argument in the function definition of main. This is because main is plainly a module level function, you only need to specify the self when you are writing a function contained inside a class (i.e. a method).

Simply remove it from the definition:

def main():


来源:https://stackoverflow.com/questions/36815974/typeerror-main-takes-exactly-1-argument-0-given

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!