Of course Python supports multi-level heritage. Your problem has nothing to do with it, You're just overwriting classes with your instances..Do this:hija = hija(p1,p2,p3,h1,h2)
implies that now the name hija It does not point to a class but rather an instance of it. You shouldn't do this, you should do something like:inst_hija = hija(p1,p2,p3,h1,h2)
However, a couple of conventions you should follow to avoid such problems:It is recommended to name classes starting with capital.Always use 4 spaces to identify each level. You mustn't use tabulations and never mix spades with tabulations. Look at you. https://www.python.org/dev/peps/pep-0008/ for more information.Your code should be something like:class Padre(object):
def __init__(self,p1,p2,p3):
self.p1 = p1
self.p2 = p2
self.p3 = p3
def funcion_metodo_clase_padre_1(self):
print self.p1,"Ejecuta una funcion/método de clase padre"
def funcion_metodo_clase_padre_2(self):
print self.p2,"Ejecuta una funcion/método de clase padre"
class Hija(Padre):
def init(self,p1,p2,p3,h1,h2):
Padre.init(self, p1,p2,p3)
self.h1 = h1
self.h2 = h2
def funcion_metodo_clase_hija_1(self):
print self.p1,"metodo hija"
class Nieta(Hija):
def init(self, p1,p2,p3,h1,h2,n1,n2):
Hija.init(self,p1,p2,p3,h1,h2)
self.n1 = n1
self.n2 = n2
def funcion_metodo_clase_nieta_1(self):
print self.p1,"metodo nieta"
p1 = 2
p2 = 4
p3 = 6
h1 = 3
h2 = 5
n1 = 10
n2 = 20
hija = Hija(p1,p2,p3,h1,h2)
hija.funcion_metodo_clase_padre_1()
hija.funcion_metodo_clase_padre_2()
hija.funcion_metodo_clase_hija_1()
nieta = Nieta(p1,p2,p3,h1,h2,n1,n2)
nieta.funcion_metodo_clase_nieta_1()
nieta.funcion_metodo_clase_padre_1()
In your case you explicitly call the method init of the father, another option is to use super()to call the parent's initializer automatically:class Hija(Padre):
def init(self,p1,p2,p3,h1,h2):
super(Hija, self).init(p1,p2,p3)
self.h1 = h1
self.h2 = h2
def funcion_metodo_clase_hija_1(self):
print self.p1,"metodo hija"
class Nieta(Hija):
def init(self, p1,p2,p3,h1,h2,n1,n2):
super(Nieta, self).init(p1,p2,p3,h1,h2)
self.n1 = n1
self.n2 = n2
def funcion_metodo_clase_nieta_1(self):
print self.p1,"metodo nieta"
Anyway, the real potential super is obtained with multiple inheritance.