python之super()函数

python之super()函数

python的构造器奇特, 使用魔方. 构造器内对基类对象的初始化同样也很奇特, 奇特到没有半点优雅!

在构造器中使用super(class, instance)返回super对象,然后再调用某个方法, 这样super对象就自动将继承链上所有的super实例迭代地调用该方法.示例:

>>> class A:
...     def __init__(self):
...             self.name='shit'
...
>>> class B(A):
...     def __init__(self):
...             super(B,self).__init__()
...             self.greet=self.name+' your shit!'
...
>>> b=B()
>>> b.greet
'shit your shit!'

原文地址:https://www.cnblogs.com/zolo/p/5848854.html