python super()

[size=medium;]python内置函数super()[/size]



super作为关键字在python2.2版本中出现的,目的是要解决子类绑定父类的问题,使用super可以降低代码的维护难度,在子类中可以方便的调用父类中的方


法。super关键字使用在new-style class中使用,形式如下:
[code="python"]super(type [, object - or - type ])

如果省略第二个参数,super 产生的object为非绑定。如果第二个参数为object,

[code="python"]instance(obj, type)
为True,如果第二个参数为type, issubclass(type2, type)为True。


典型的使用方法为:

[code="python"]class C(B):
        def methon(self, arg):
                super(C, self).method(arg)

使用super()的两种典型情况:


情况一:在单一继承的类结构中,使用super引用父类,提高代码的维护效率。

情况二:he second use case is to support cooperative multiple inheritance in a
dynamic execution environment.  This use case is unique to Python and is
not found in statically compiled languages or languages that only
support
single inheritance.  This makes it possible to implement “diamond
diagrams”
where multiple base classes implement the same method.  Good design
dictates
that this method have the same calling signature in every case (because
the
order of calls is determined at runtime, because that order adapts
to changes in the class hierarchy, and because that order can include
sibling classes that are unknown prior to runtime).(不是很懂,所以把英文写上,大意为在动态运行环境中,支持多重继承,可以调用多个父类的相同方法,如果不对请指正啊。)

 

你可能感兴趣的:(C++,c,python,C#)