问题
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