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

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


class Dog:

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

    def run(self):
        eat(2)

Dog().run()

结果:

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

再看看加个self的情况:

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

相关文章:

  • 2023-03-20
  • 2022-12-23
  • 2021-11-16
  • 2022-12-23
  • 2022-12-23
  • 2021-04-11
  • 2021-12-31
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-07-21
  • 2021-12-23
  • 2022-01-16
  • 2022-12-23
  • 2023-03-17
相关资源
相似解决方案