Super, New-style and classic classes


super ( type [, object-or-type ] )

Return a proxy object that delegates method calls to a parent or siblingclass of type. This is useful for accessing inherited methods that havebeen overridden in a class. The search order is same as that used bygetattr() except that the type itself is skipped.

The __mro__ attribute of the type lists the methodresolution search order used by both getattr() and super(). Theattribute is dynamic and can change whenever the inheritance hierarchy isupdated.

If the second argument is omitted, the super object returned is unbound. Ifthe second argument is an object, isinstance(obj, type) must be true. Ifthe second argument is a type, issubclass(type2, type) must be true (thisis useful for classmethods).

Note

super() only works for new-style classes.

There are two typical use cases for super. In a class hierarchy withsingle inheritance, super can be used to refer to parent classes withoutnaming them explicitly, thus making the code more maintainable. This useclosely parallels the use of super in other programming languages.

The second use case is to support cooperative multiple inheritance in adynamic execution environment. This use case is unique to Python and isnot found in statically compiled languages or languages that only supportsingle inheritance. This makes it possible to implement “diamond diagrams”where multiple base classes implement the same method. Good design dictatesthat this method have the same calling signature in every case (because theorder of calls is determined at runtime, because that order adaptsto changes in the class hierarchy, and because that order can includesibling classes that are unknown prior to runtime).

For both use cases, a typical superclass call looks like this:

class C(B):
    def method(self, arg):
        super(C, self).method(arg)

Note that super() is implemented as part of the binding process forexplicit dotted attribute lookups such as super().__getitem__(name).It does so by implementing its own __getattribute__() method for searchingclasses in a predictable order that supports cooperative multiple inheritance.Accordingly, super() is undefined for implicit lookups using statements oroperators such as super()[name].

Also note that super() is not limited to use inside methods. The twoargument form specifies the arguments exactly and makes the appropriatereferences.

For practical suggestions on how to design cooperative classes usingsuper(), see guide to using super().

New in version 2.2.


3.3. New-style and classic classes

Classes and instances come in two flavors: old-style (or classic) and new-style.

Up to Python 2.1 the concept of class was unrelated to the concept oftype, and old-style classes were the only flavor available. For anold-style class, the statement x.__class__ provides the class of x, buttype(x) is always <type 'instance'>. This reflects the fact that allold-style instances, independent of their class, are implemented with a singlebuilt-in type, called instance.

New-style classes were introduced in Python 2.2 to unify the concepts ofclass and type. A new-style class is simply a user-defined type,no more, no less. If x is an instance of a new-style class, then type(x)is typically the same as x.__class__ (although this is not guaranteed – anew-style class instance is permitted to override the value returned forx.__class__).

The major motivation for introducing new-style classes is to provide a unifiedobject model with a full meta-model. It also has a number of practicalbenefits, like the ability to subclass most built-in types, or the introductionof “descriptors”, which enable computed properties.

For compatibility reasons, classes are still old-style by default. New-styleclasses are created by specifying another new-style class (i.e. a type) as aparent class, or the “top-level type” object if no other parent isneeded. The behaviour of new-style classes differs from that of old-styleclasses in a number of important details in addition to what type()returns. Some of these changes are fundamental to the new object model, likethe way special methods are invoked. Others are “fixes” that could not beimplemented before for compatibility concerns, like the method resolution orderin case of multiple inheritance.

While this manual aims to provide comprehensive coverage of Python’s classmechanics, it may still be lacking in some areas when it comes to its coverageof new-style classes. Please see https://www.python.org/doc/newstyle/ forsources of additional information.

Old-style classes are removed in Python 3, leaving only new-style classes.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值