所有方法的第一个参数必须是 self,self 表示类是实例本身,类似于 c++ 中的 this 指针。当然我们也可以讲 self 写成其它名字:

1 class MyClass(object):
2     i = 123
3     def f(this):#self写成this也是正确的
4         return 'hello'
5 
6 gel = MyClass()
7 print(gel.i)
8 print(gel.f())
View Code

相关文章:

  • 2021-10-09
猜你喜欢
  • 2021-06-12
  • 2021-09-21
  • 2021-12-29
  • 2021-09-06
  • 2021-12-22
  • 2021-08-29
相关资源
相似解决方案