python super原理,不是指父类
2024-10-19 19:39:48
class a(object):
def __init__(self):
print('in a')
class b(a):
def __init__(self):
print('in b')
super().__init__()
print('out b')
class c(a):
def __init__(self):
print('in c')
super().__init__()
print('out c') class d(b, c):
def __init__(self):
print('in d')
super().__init__()
print('out d') a = d()
print(a.__class__.mro())
每个对象都有个属性__class__.mro()
mro是method resolve order的缩写
代码中是写的super().__init__()
super()这种写法是super(当前类名,self)的简写
然后我们直接来看看这段代码执行的结果
[Running] python "d:\pyqt5\signal.py"
in d
in b
in c
in a
out c
out b
out d
(<class '__main__.d'>, <class '__main__.b'>, <class '__main__.c'>, <class '__main__.a'>, <class 'object'>)
为啥中间会输出c?
c并不是b的父类啊
super其实干的是下面这件事
def super(cls, inst):
mro = inst.__class__.mro()
return mro[mro.index(cls) + 1]
就是在mro列表中去找下一个类,所以会产生这种输出
最新文章
- 纯html页面之间传参
- Swift:属性观察器
- 开发Windows Phone应用程序之后的感觉
- dg
- css教程
- openstack简介
- JSP&;Servlet学习手册
- Spring MVC 时间字符串 @PathVariable获取
- servlet其工作原理和例子证明
- JavaScript 中 if 条件判断
- 简单说下C#变量的作用域
- IT女孩特不烦恼---九月实习总结
- vue-These relative modules were not found
- BZOJ 4282(慎二的随机数列
- 【读书笔记】iOS-基带攻击
- vs远程调试 转http://www.cnblogs.com/magicchaiy/archive/2013/05/28/3088274.html
- jupyter notebook的路径
- [设备]Linux设备是否可以被多个进程或者线程同时Open?
- ctex2.9.2输出中文
- MetaPost使用