Python中self的理解
来源:互联网 发布:淘宝自动刷单软件 编辑:程序博客网 时间:2024/06/03 02:27
- 今天比较疑惑python中self的用法。参考了http://blog.csdn.net/lengyue_wy/article/details/6715185里面说:
假如你有一个类称为MyClass和这个类的一个实例MyObject。当你调用这个对象的方法MyObject.method(arg1,arg2)的时候,这会由Python自动转为MyClass.method(MyObject, arg1,arg2)——这就是self的原理了。 - 于是我又参考了http://www.cnblogs.com/vamei/archive/2012/06/02/2532018.html
并且改变了其中的例程进行理解。 - 例程1
class Human(object): laugh = 'hahahaha' def show_laugh(self): #self.laugh="hihihihihi" print self.laugh def laugh_100th(self): for i in range(100): self.show_laugh()li_lei = Human() li_lei.show_laugh()print Human.laugh
输出为
hahahahahahahaha
li_lei.show_laugh()等效于Humen.show_laugh(li_lei)
带入后有
def show_laugh(li_lei): print li_lei.laugh
说明实例属性在没定义的时候和类属性是一样的,即实例属性继承了类属性。
5 例程2
class Human(object): laugh = 'hahahaha' def show_laugh(self): self.laugh="hihihihihi" print self.laugh def laugh_100th(self): for i in range(100): self.show_laugh()li_lei = Human() li_lei.show_laugh()print Human.laugh
输出为
hihihihihihahahaha
说明实例属性在自定义之后和类属性是不同的,并且实例属性的自定义并不会改变类属性。
- 总之一点,self在可以假想为在后面定义的某个实实在在的实例对象,也相当于我先定义一个预先在这里放着,以后要用的时候你再带入就好了。
- 实例对象在其属性没有定义的时候采用的是类属性的值,当一旦定义了自己的属性就不再用类属性,并且定义自己的属性并不会改变类属性。
0 0
- Python中self的理解
- Python中self的理解
- Python中self的理解
- Python中self的理解
- Python中默认参数self的理解
- Python中默认参数self的理解
- Python中self的理解(一)
- Python中self的理解(二)
- Python中默认参数self的理解
- 关于Python中定义类时 self 的理解
- Python中默认参数self的理解[转]
- Python中self的含义
- python中self的作用
- Python中self的含义
- Python中self的含义
- [python]对self和_init_的理解
- python学习过程对self的理解
- python中的self理解
- 第六题(格式化)
- 【转发】Visual Studio 2013 如何关闭调试而不关闭IIS Express
- ubuntu 安装 redis 与并配置php扩展
- Debian下MySQL安装
- Linux下C++的Helloword
- Python中self的理解
- 第十节 常用组件 之 Lookup 创建查找转换
- ExtJs通过后台动态创建树
- 小户型特色涂鸦装饰 55平时尚三口之家
- iOS帐号、证书之漫谈(三)—— 申请Apple ID
- IOS测试之sonar检查ios代码质量
- [Unity3D]Unity中的Time
- mongodb 的一些常用命令。
- 第五题(重写,重载)