Wednesday, November 26, 2008

The order of the Python's class inheritance

Every OOP language provide the inheritance feature.In Python one subclass could have multiple super class,For example:we have two class A and B,now we create another class C,it's inherit from A and B:
class A:
def method1(self):
print "this is the method1 of class A"
class B:
def method1(self):
print "this is the method2 of class B"
class C(A,B): #class C inherit from A and B
pass


c=C()
c.method1()#Now the question is which method will be call,A or B
#The result is:
this is the method1 of class A

That means,the inheritance rest with which super class in the starting location,in this example,class A in the zero position,so the method1 in class A will be call instead of class B

No comments: