python 在类的成员函数里调用成员函数
在 Python 中,在类的成员函数里调用成员函数,可以使用 self 关键字来引用该类的实例。例如:
class MyClass:
def func1(self):
print("I am func1")
def func2(self):
self.func1()
print("I am func2")
obj = MyClass()
obj.func2()
输出结果为:
I am func1
I am func2
其中,obj.func2() 调用了 func2 方法,func2 方法使用 self.func1() 调用了 func1 方法。注意,self 关键字在调用类的成员函数时是必须的。
原文地址: https://www.cveoy.top/t/topic/X4g 著作权归作者所有。请勿转载和采集!