super() 函数是用于调用父类(超类)的一个方法。
class singer(people):
super(singer, self).show_my_power(),找singer的父类people,再回到子类singer中。多个继承时,有点像先进后出。下面这个例子,artist实例化后,先找到子类singer(左边),输出"before_super_singer_first",再找到子类actor(右边),输出"before_super_actor_second",然后到达父类people,输出"people"。父类结束后,先到子类actor,输出"after_super_actor_third",再到子类singer,输出"after_super_singer_fourth"。(singer先进actor再进,actor先出singer最后出)
class parent(object):
def show_my_power(self):
print("parent")
class singer(parent):
def show_my_power(self):
print("before_super_singer_first")
super (singer, self).show_my_power()
print("after_super_singer_fourth")
class actor(parent):
def show_my_power(self):
print("before_super_actor_second")
super (actor, self).show_my_power()
print("after_super_actor_third")
class artist(singer, actor):
pass
if __name__ == "__main__":
a = artist() #实例化
a.show_my_power()
输出结果
before_super_singer_first
before_super_actor_second
parent
after_super_actor_third
after_super_singer_fourth
https://blog.csdn.net/Quincuntial/article/details/88663237
http://www.sohu.com/a/331661377_571478