标签:体系 comment 定义 tail obj 继承 算法 机制 dso
转:https://blog.csdn.net/u011467044/article/details/52205961
python语言与C++有相似的类继承,在类定义时,python中会自定义第一个self,类似C++中this指针,指向对象自身。
python简单的类举例:
- >>> class hello(object):
- ... def print_c():
- ... print"hello world!"
- >>> hello().print_c()
- hello world!
当然在实际中不可避免的需要类的继承,子类继承父类,正常如下:
- >>> class child(hello):
- ... def print_c(self):
- ... hello().print_c()
- ...
- >>> child().print_c()
- hello world!
在python中还提供了super()机制,例子如下:
- >>> class hello(object):
- ... def print_c(self):
- ... print"hello world!"
- ...
- >>> class child(hello):
- ... def print_c(self):
- ... super(child,self).print_c()
- ...
- >>> child().print_c()
- hello world!
第一眼看过来是不是感觉一样?
在python中引入super()的目的是保证相同的基类只初始化一次(注意:
1super ()机制是用来解决多重继承的,对于直接调用父类名是没有问题的,但在之后根据前人的经验就是:要么都用类名调用,要么就全部用super(),不要混合的用,由此为人做事还是要专一的嘛O(∩_∩)O~
2 super()继承只能用于新式类,用于经典类时就会报错。
新式类:必须有继承的类,如果无继承的,则继承object
经典类:没有父类,如果此时调用super就会出现错误:『super() argument 1 must be type, not classobj)
好,再举一个例子
- class parent1(object):
- def __init__(self):
- print ‘is parent1‘
- print ‘goes parent1‘
-
- class parent2(object):
- def __init__(self):
- print ‘is parent2‘
- print ‘goes parent2‘
-
- class child1(parent1):
- def __init__(self):
- print‘is child1‘
- parent.__init__(self)
- print ‘goes child1‘
-
- class child2 (parent1) :
- def __init__(self):
- print ‘is child2‘
- parent.__init__(self)
- print ‘goes child2‘
-
- class child3(parent2):
- def __init__(self):
- print ‘is child3‘
- parent2.__init__(self)
- print ‘goes child3‘
-
- class grandson(child3,child2,child1):
- def __init__(self):
- print ‘is grandson‘
- child1.__init__(self)
- child2.__init__(self)
- child3.__init__(self)
- print‘goes grandson‘
-
-
- if __name__==‘__main__‘:
- grandson()
- is grandson
- is child1
- is parent1
- goes parent1
- goes child1
- is child2
- is parent1
- goes parent1
- goes child2
- is child3
- is parent2
- goes parent2
- goes child3
- goes grandson
- 好了,在这儿发现什么问题了没有?对,基类parent1被多次执行,而有时我们只希望公共的类只被执行一次,那么此时我们引入super()机制查看效果:
- <span style="font-family: Arial, Helvetica, sans-serif;">class parent1(object):</span>
- def __init__(self):
- super(parent1, self).__init__()
- print ‘is parent1‘
- print ‘goes parent1‘
-
- class parent2(object):
- def __init__(self):
- super(parent2, self).__init__()
- print ‘is parent2‘
- print ‘goes parent2‘
-
- class child1(parent1):
- def __init__(self):
- print‘is child1‘
-
- super(child1, self).__init__()
- print ‘goes child1‘
-
- class child2 (parent1) :
- def __init__(self):
- print ‘is child2‘
-
- super(child2, self).__init__()
- print ‘goes child2‘
-
- class child3(parent2):
- def __init__(self):
- print ‘is child3‘
-
- super(child3, self).__init__()
- print ‘goes child3‘
-
- class grandson(child3,child2,child1):
- def __init__(self):
- print ‘is grandson‘
-
-
-
- super(grandson, self).__init__()
-
- print‘goes grandson‘
-
-
- if __name__==‘__main__‘:
- grandson()
- is grandson
- is child3
- is child2
- is child1
- is parent1
- goes parent1
- goes child1
- goes child2
- is parent2
- goes parent2
- goes child3
- goes grandson
结果很明显,公共基类parent1只被执行一次。
grandson类的继承体系如下图所示:
- object
- |
- / \
- P1 P2
- / \ |
- C2 C3
所以对于类的继承体系,我们可以看做一个图,而每一个类看做一个节点,那么super()机制的执行顺序是按照图的广度优先搜索算法查找super()的父类。
后续总结:
1. super()是一个类名而非函数,super(class, self)事实上调用了super类的初始化函数,产生了一个super对象;
2 super()机制必须要用新式类,否则会报错;
3 super()或直接父类调用最好只选择一种形式。
python中super用法
标签:体系 comment 定义 tail obj 继承 算法 机制 dso
原文地址:https://www.cnblogs.com/shmily2018/p/9073880.html