TypeError:main()恰好接受1个参数(给定0) [英] TypeError: main() takes exactly 1 argument (0 given)

查看:104
本文介绍了TypeError:main()恰好接受1个参数(给定0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Python 2.7.11的类文件中创建一个main()并运行它,但是Python声称我需要传递main()一个参数.

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)

推荐答案

在这种情况下,main的函数定义中不需要self自变量.这是因为main显然是模块级别的函数,只需要在编写包含在类内的函数(即方法)时指定self.

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():

这篇关于TypeError:main()恰好接受1个参数(给定0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆