python 类中的方法互相调用为什么是self.function()形式

 因为,不加self, 你可能会执行类外的同名函数eat

def eat(thing):
    print(thing,'--->执行了类外函数')


class Dog:

    def eat(self, thing):
        print('执行了类内函数', thing)

    def run(self):
        eat(2)

Dog().run()

结果:

再看看加个self的情况:

猜你喜欢

转载自blog.csdn.net/sinat_38068807/article/details/86531214
今日推荐