且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python:由实例对象调用方法:“missing 1 required positional argument:'self'”

更新时间:2022-06-13 15:16:29

call_uselessmethod 要求首先有一个实例 Class2 。但是,这样做:

call_uselessmethod requires that there first be an instance of Class2 before you use it. However, by doing this:

k = Class2

您不会将 k 分配给 Class2 的实例, Class2 本身。

you are not assigning k to an instance of Class2 but rather Class2 itself.

创建 Class2 ,在类名后添加()

To create an instance of Class2, add () after the class name:

k = Class2()
k.call_uselessmethod()

现在,您的代码将工作,因为 k 指向 Class2 的实例。

Now, your code will work because k points to an instance of Class2 like it should.