0

Can any one help me to inherit multiple classes in python.

Let say I have three classes in three different modules.

a.py :

class A():
   def __init__(self):
      pass

   def fruit(self):
     print "Apple is a fruit"

b.py :

class B():
    def __init__(self):
       pass

    def vegetable(self):
       print "Potato is a vegetable"

c.py :

class C():
  def __init__(self):
      pass

  def vehicle(self):
     print "Car is a vehicle"

I just want to configure all these class names in a file like , a.A , b.B and c.C. then read the configuration file and load the configured modules and inherit those modules( Multiple Inheritance ).

utility.py

Read configuration and get the modules.

class Utility(INHERIT_CONFIGURED_MODULES):
    def __init__(self):
       pass

obj = Utility()
obj.fruit()
obj.vegetable()
obj.vehicle()

Now ,If I create one more module and configure that in my configuration file,then I should access its methods using the objected created for Utility class.

Hope its clear.Please guide me to achieve this.

Aleksandr Kovalev
  • 3,508
  • 4
  • 34
  • 36
karthi_ms
  • 5,568
  • 11
  • 38
  • 39
  • 2
    What are you actually trying to do? There's probably a much cleaner way to implement it. – Colonel Thirty Two May 26 '15 at 14:08
  • @Philiip. thanks for the mention. I solved the problem. modules = ['a.A','b.B','c.C'] collect = [] for module in modules: (mod,cls) = module.split('.') imp=__import__(mod,globals(), locals(),[cls],-1) collect.append(getattr(imp,cls)) class Utility(): def __init__(self): pass obj=Utility() Utility.__bases__ = (tuple(collect)) obj.vehicle() obj.fruit() – karthi_ms May 26 '15 at 15:39

0 Answers0