python 那些待深入理解的东西
2017-03-10 00:13
393 查看
class Father:#2.7版本的老式类 def __init__(self): pass def function(self): print(type(self)) print(isinstance(self, Son)) print(isinstance(self, Father)) print(self.mlist) class Son(Father): def __init__(self): self.mlist = [1, 2, 3] def function(self): Father.function(self) s = Son() s.function()
输出:
<type 'instance'>
True
True
[1, 2, 3]
这个例子程序中,有一点颠覆了我最近学习python后建立在大脑中的知识大厦。原来以为python中类的方法中的self关键字只是一个书写习惯,在调用类的方法的时候根本无需关心。然后在这个例子中却发现self的值使Father的function方法中的内容正常执行了。非常奇妙。
由此,这个特性也用来解决了用户重载新式类中__getattribute__方法时无限递归__getattribute__方法自己的bug
相关文章推荐
- Python 深入理解yield
- 深入理解python之self
- 转载:Python单元测试——深入理解unittest
- Python 深入理解yield
- 深入理解python之self
- Python天天美味(25) - 深入理解yield
- 深入理解python之self
- Python中的魔法方法深入理解
- 深入理解Python 代码优化详解
- <深入理解Python中文版>读书笔记
- Python天天美味(25) - 深入理解yield
- Python天天美味(25) - 深入理解yield
- Python单元测试——深入理解unittest
- 深入理解Java虚拟机到底是什么东西
- Python 深入理解
- 深入学习Django源码基础1 - 深刻理解Python中的元类(metaclass)
- python基础(5):深入理解 python 中的赋值、引用、拷贝、作用域
- 深入理解python之self
- 深入理解Python 代码优化详解