python:missing 1 required positional argument: 'self' 的error

问题描述:

class test:
    def test01(self):
        pass

if __name__ == "__main__":
    test.test01()

如上代码执行时会报错:missing 1 required positional argument: 'self' ,问题原因如下:

对象的声明需要括号。而类的声明括号可有可无

定义在自定义类中的方法需要一个默认的self参数。错误提示没有self 就是说明这个类的对象没有创建成功。

修改后的代码片段:

if __name__ == "__main__":
    test().test01()

本文转载自:https://www.cnblogs.com/walterwsj/p/11627439.html