Python Tutorial

Monday, September 3, 2012

Python multiple inheritance, MRO



Python support multiple inheritance. For multiple inheritance we need to know MRO(Method Resolution Order) .I recommend to read this very interesting paper for how MRO works. Python has builtin method class.mro() method


 
class Arithmetic(object):
    def add_num(self,a,b):
        print a+b

class String(object):
    def add_num(self,a,b):
        print a-b

    def print_string_upper(self,data):
        print data.upper()

class Common(Arithmetic,String):
    def print_class_c(self):
        print "This is C"



print Common.mro()

mih=Common()
mih.add_num(2,3) #Following MRO 
mih.print_string_upper("python")
mih.print_class_c() 




Output:
[<class '__main__.Common'>, <class '__main__.Arithmetic'>, <class '__main__.String'>, <type 'object'>]
5
PYTHON
This is C

0 comments:

Post a Comment