【python】类中的self
2016-10-25 21:16
381 查看
在python的类中,经常会写self,代表对象自己。如下例:
实际上,这只是一种约定的习惯写法。其实不写self,写其他的也是能够正确运行的。比如下面这样:
#coding=utf-8 class Foo: def __init__(self, name): self.name = name def hi(self): print self.name if __name__ == '__main__': foo01 = Foo('letian') foo01.hi() print type(Foo) print type(foo01) print id(Foo) print id(foo01)
实际上,这只是一种约定的习惯写法。其实不写self,写其他的也是能够正确运行的。比如下面这样:
#coding=utf-8 class Foo: def __init__(a, name): a.name = name def hi(b): print b.name if __name__ == '__main__': foo01 = Foo('letian') foo01.hi() print type(Foo) print type(foo01) print id(Foo) print id(foo01)
相关文章推荐
- python self,cls,decorator的理解
- python __main__ 和self代表什么 ?
- python __str__(self) __unicode__(self)
- Python self 详解
- Python在定义类方法时为什么要显示声明self参数
- python self
- Cellular Automaton-Python for Programmer : UC Berkeley SelfPace Course
- 【python】Python中的self
- Self Describing Numbers自描述数字问题(Python版)
- Python Self
- 关于python中self
- Python中self的理解
- 深入理解python之self
- python里的self
- python 参数列表中的self 显式不等于冗余
- python中类中函数参数self
- python self __init__
- python 深拷贝与浅拷贝以及list.append( self )
- python中的self
- python中self,cls