The difference between a new class and a classic class:
###################################################
Class Classicclass ():
Pass
Class Newstyleclass (object):
Pass
X1 = Classicclass ()
x2 = Newstyleclass ()
Print x1.__class__, type (x1)
Print x2.__class__, type (x2)
Output Result:
__main__. Classicclass <type ' instance ' >
<class ' __main__. Newstyleclass ' > <class ' __main__. Newstyleclass ' >
The inheritance of classic class is depth first, and the inheritance of new class is breadth first.
####################################################################
The subclass and derivation motives are designed to save development events and make it easier to do related designs.
One of the more powerful features of OOP is the ability to extend it or modify it using an already defined class, without affecting other snippets of code that use existing classes in the system.
Ood allows class features to be integrated within descendant classes or subclasses. These subclasses inherit their core properties from the base class (or ancestor class, superclass). Furthermore, these derivations may be extended to multiple generations.
Related classes in a hierarchical derivation relationship (or vertically adjacent to a class tree) are parent and subclass relationships. Deriving these classes from the same parent class (or horizontally ringing in the class tree) is a sibling relationship. The parent class and all high-level classes are considered ancestors.
Inheritance describes how the properties of a base class are "inherited" to derived classes. A subclass can inherit any property of its base class, whether it is a data property or a method.
For classes, functions \ methods, and modules, the document string is unique, so the special attribute __doc__ does not inherit from the base class. 、
inheritance override method:super () built-in method;
The beauty of using super () is that you don't need to explicitly give any base class name ... , it can help you do it! Use the focus of super () so that you do not need to explicitly provide the parent class. This means that if you change the class inheritance, you only need to modify one line of code (the class statement itself) without having to find the names of all the modified classes in a lot of code.
Multiple inheritance:
Python, like C + +, supports multiple inheritance. Although the concept is easy, the hard work is that if a subclass invokes a property that itself does not have a definition, it is in what order to find it in the parent class, especially if many of the parent classes contain the same name attribute.
For classic and modern classes, the order in which attributes are found is different. Now let's look at two different manifestations of the classic and new classes:
Classic class:
#! /usr/bin/python#-*-coding:utf-8-*-class P1 (): def foo (self): print ' P1-foo ' class P2 (): def foo (self): print ' P2-foo ' def Bar (self): print ' P2-bar ' class C1 (P1,P2): passclass C2 (P1,P2): def bar (self) : print ' C2-bar ' class D (C1,C2): passif __name__ = = ' __main__ ': D=d () d.foo () D.bar ()
Results of execution:
P1-foo
P2-bar
The code example, drawing a diagram, easy to understand:
Judging from the output of the classic class above,
When instance D calls Foo (), the search order is d = = C1 = P1,
When instance D calls bar (), the search order is d = C1 = P1 = P2
Summary: The Classic class Search method is based on "left to right, depth first" way to find properties. D First look for whether it has the Foo method, no find the nearest parent class C1 the method, if not, continue to look up until the method is found in P1, find the end.
New class:
#! /usr/bin/python#-*-coding:utf-8-*-class P1 (object): def foo (self): print ' P1-foo ' class P2 (object): def foo: print ' P2-foo ' def Bar (self): print ' P2-bar ' class C1 (P1,P2): Pass Class C2 (P1,P2): def Bar (self): print ' C2-bar ' class D (C1,C2): pass if __name__ = = ' __main__ ‘:
Print d.__mro__ #只有新式类有__mro__属性, tell what the lookup order is D=d () d.foo () D.bar ()
Results of execution:
(<class ' __main__. D ';, <class ' __main__. C1 ';, <class ' __main__. C2 ';, <class ' __main__. P1 ';, <class ' __main__. P2 ';, <type ' object ' >)
P1-foo
C2-bar
Judging from the output of the above-mentioned new class,
When instance D calls Foo (), the search order is d = C1 = C2 = P1
When instance D calls bar (), the search order is d = = C1 = C2
Summary: The search for the new class is based on the "breadth first" approach to finding attributes.
python-subclasses and derivations, inheritance