Python Multiple inheritance

Source: Internet
Author: User
Tags class definition

http://blog.csdn.net/pipisorry/article/details/46381341

There is typical use cases forSuper:

In a class hierarchy Withsingle inheritance, Super can is used to refer to parent classes withoutnaming them expl Icitly, thus making the code more maintainable. This useclosely parallels the use ofSuper in other programming languages.

The second use case was to support cooperative multiple inheritance in adynamic execution environment. This use case is a unique to Python and IsNot found in statically compiled languages or languages. Heritance. This makes it possible to implement "diamond diagrams" where multiple base classes implement the same method. Good design Dictatesthat This method has the same calling signature in every case (because theorder of calls are determine D at runtime, because this order adaptsto changes in the class hierarchy, and because that order can includesibling classe s that is unknown prior to runtime).

For both use cases,a typical superclass call looks like this:
class C (B):    def Method (self,arg):        Super (). Method (arg)    # This does the same thing as:S-uper (C, Self). Method (ARG)

[super]


Class Inheritance Instance

Parent class Definition:

classParent1(Object):    defOn_Start( Self):        Print(' Do something ')classParent2(Object):    defOn_Start( Self):        Print(' do something Else ')classParent3(Object):    Pass
Subclass Definition 1:

class Child(Parent1, Parent2, Parent3):    defOn_Start( Self):         forBaseinchChild.__bases__:            Try:                Base.on_start ( Self)exceptAttributeerror:                # handle that one of the those does not having that method                Print(' {} ' does not has an ' on_start '. Format (base.__name__))
 Child (). On_Start ()  # c = Child (); C.on_start ()


Result output:
Do something
Do something else
"Parent3" does not having an "On_Start"
Subclass Definition 2

class Child(Parent1, Parent2):    defOn_Start( Self):        Super(Child, Self). On_Start ()Super(Parent1, Self). On_Start ()class Child(Parent1, Parent2):    defOn_Start( Self):        Parent1.on_start ( Self) Parent2.on_start ( Self) child (). On_Start ()# c = Child (); C.on_start ()Two results are output as: do Somethingdo something else

Note:

1. Since both of the parents implements the same method, would just be the super same as the first parent inherited, from L EFT to right (for your code, Parent1 ). Calling functions with is super impossible.

2. Note the usage in subclass definition 2 1

[Python multiple Inheritance:call super on all]


The problem of attention

按类名访问is equivalent to the C language before the GOTO statement ... Jumping, and then using super sequential access, there is a problem.

So the advice is to either keep using super it or keep it按照类名访问

Best implementations:
    1. Avoid multiple inheritance
    2. Super uses consistent
    3. Don't mix classic and modern classes
    4. Check the class hierarchy when calling the parent class
[Advanced features of Python class inheritance]

from:http://blog.csdn.net/pipisorry/article/details/46381341

Why does Ref:python inherit the object class?


Python Multiple inheritance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.