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).(不是很懂,所以把英文写上,大意为在动态运行环境中,支持多重继承,可以调用多个父类的相同方法,如果不对请指正啊。)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值