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:
- Avoid multiple inheritance
- Super uses consistent
- Don't mix classic and modern classes
- 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