Hi, I am studying the operation of classes and legacy in python, and trying to create small programs as exercises to test the theory. I do not get the program to work. Is it possible to create a subclass within another subclass, which inherits the attributes of the parent parent class and the daughter subclass? Or on the contrary, only subclasses are possible from the main parent class?
I have included the code for better understanding. cracia for your help.
class padre(object):
###Este es el constructor de atributos de la clase padre
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"
p1 = 2
p2 = 4
p3 = 6
###padre(2,4,6)
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"
h1 = 3
h2 = 5
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()
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"
n1 = 10
n2 = 20
nieta = nieta(p1,p2,p3,h1,h2,n1,n2)
nieta.funcion_metodo_clase_nieta_1()
This is the error that I get when compiling:
2 Ejecuta una funcion/método de clase padre 4 Ejecuta una funcion/método de clase padre 2 metodo hija Traceback (most recent call last): File "python", line 54, in <module> TypeError: Error when calling the metaclass bases __init__() takes exactly 6 arguments (4 given)