The end of the article is going of this series, considering, or have to add a chapter, for a further modify it

in a code, on the basis of further modification, became the following procedures, reader, please study the program:

code is as follows:


#! The/usr/bin/env python
#coding: utf-8

class Person:
      Def __init__ (self, name, email) :
              The self. The name=name
              Self. Email=email

class Programmer (Person) :
      Def __init__ (self, name, email, lang, system and website) :
              Person. __init__ (self, name, email)
              Self. Lang lang=
              The self. The system=system
              The self. The website=website

class Pythoner (Programmer) :
      Def __init__ (self, name, email) :
              Programmer. __init__ (self, name, email, "python", "Ubuntu", "qiwsir. Making. IO")

if __name__=="__main__" :
      Writer=Pythoner (" qiwsir ", "qiwsir@gmail.com")
      Print "name=", the writer. The name
      Print "lang=", writer. Lang,
      Print "email=", writer. Email
      Print "system=", writer system
      Print "website=", writer. Website

#running results

name=qiwsir
Lang=python
Email=qiwsir@gmail.com
System=Ubuntu
Website=qiwsir. Making. IO

  Very pleased with the results, and then inheritance in the program: Pythoner <- Programmer <- the Person, it is not hard to see from the above process, inheritance can reduce code duplication, yes code more concise. In addition, in succession, also can to the default parameters in the function assignment.

in order to highlight the explore of succession, or use the simple class to do the experiment.

redundant B
code is as follows:


#! The/usr/bin/env python
#coding: utf-8

A class A:
      Def __init__ (self) :
              Print "aaa"

class B (A) :
      Pass

if __name__=="__main__" :
      A=(a)
      B=b ()

#running results

aaaAaa

  B inherits A, without any modification to inherit, can need not write anything, B or B is essentially A redundant. In the process of real programming, no such written, here just to show the reader the meaning of the inheritance.

code is as follows:


First inherit the effective ##

#! The/usr/bin/env python
#coding: utf-8

A class A:
      Def __init__ (self) :
              Print "aaa"

class B:
      Def __init__ (self) :
              Print "BBB"

class C1 (A, B) :
      Pass

class C2 (B, A) :
      Pass

if __name__=="__main__" :
      Print "A - >" ,
      A=(a)
      Print "B - >" ,
      B=b ()
      Print "C1 (A, B) - >" ,
      C1=c1 ()
      Print "C2 (B, A) - >" ,
      C2=c2 ()

#running results

A - >Aaa
B - >BBB
C1 (A, B) - >Aaa
C2 (B, A) - >BBB

  Whether the reader attention, class C1 inherited two classes A, B; Class C2 also inherited two classes, but writing order is A bit difference (B, A). Can be seen from the results, when more than a subclass inherits the parent class for the constructor __init__ (), only the first can be inherited, the second is, etc. So, in general, not in the program of the constructor and multiple inheritance, but you can relay inheritance, like the preceding more real code.

other ways of inheritance

code is as follows:


#! The/usr/bin/env python
#coding: utf-8

A class A:
      Def __init__ (self) :
              Print "aaa"
      Def amethod (self) :
              Method a "print"

class B (A) :
      Def __init__ (self) :
              Print "BBB"

If __name__=="__main__" :
      Print "A - >"
      A=(a)
      A.a method ()
      Print "B - >"
      B=b ()
      B.a method ()

#running results

A - >
Aaa
Method a
B - >
BBB
Method a

  Said in order to understand the above situation, and drew a diagram, however, after I finished, regret, it seems more confused look at this picture. How wear also drew, or posted, if you can help to understand better.

A instance and calls, not much said. Focus on B, class B inherited A, at the same time, in the constructor did it by himself, B is B's constructor is executed, according to B will not perform A content, however, A and A amethod (self) method, B has inherited this method. When using an instance of class B calls this method, can succeed: b.a method ()

this is the inheritance method and a method is called.

inheritance, from bottom to top level 1 level 1 to find the corresponding inheritance object, found the inheritance is. If you have same name? In what order to find?

a application online:

in Python, can undertake multiple inheritance, this time should pay attention to the order of the search, started out as a subcategory, followed by the same class parent category from left to right to search for, and then to more upper parent category from left to right to search for the same class, until reach the top.

code example:

code is as follows:


Class A (object) :
      Def method1 (self) :
              Print (' arjun ethod1 ')

      Def an (self) :
              Print (' arjun ethod2 ')

class B (A) :
      Def method3 (self) :
              Print (' B.m ethod3 ')

A class C (A) :
      Def an (self) :
              Print (' c. ethod2 ')

      Def method3 (self) :
              Print (' c. ethod3 ')

a class D (B, C) :
      Def method4 (self) :
              Print (' c. ethod4 ')

d=d ()
D.m ethod4 (#) found in D, c. ethod4
D.m ethod3 () #D - >B in order to find, B.m ethod3
D.m ethod2 () #D - >B - >C in order to find, c. ethod2
D.m ethod1 () #D - >B - >C - >Find A sequence, arjun ethod1

  Be sure to please the real learners to compare each class of each method, in order to find the corresponding output. To understand the order of the inheritance. Learning, be about to drip.

you might be interested in the article:

  • python class inheritance usage analysis
  • python multiple inheritance new algorithm C3 introduction
  • wxPython window the inheritance mechanism of instance analysis
  • python examples of multiple inheritance on
  • python implementation class instance
  • python PyUnit unit test instance of
  • python USES MySQLdb for python database operation tutorial
  • python MySQLdb garbled words solution
  • python multiple inheritance instance


This concludes the body part