python subclass within another subclass

0

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)
    
asked by dipdip 04.06.2017 в 19:48
source

1 answer

3

Of course Python supports multi-level inheritance. Your problem has nothing to do with that, you are simply overwriting the classes with their instances .

Do this:

hija = hija(p1,p2,p3,h1,h2) 

implies that now the name hija does not point to a class but an instance of it. You should not do this, you should do something like:

inst_hija =  hija(p1,p2,p3,h1,h2) 

However, a couple of conventions that you should follow to avoid problems of this type:

  • It is recommended to name classes starting with uppercase.
  • Always use 4 spaces to devise between each level. You should not use tabs and never mix spicaios with tabs.

Watch PEP 8 for more information.

Your code should be something like this:

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 parent, another option is to use super() that is responsible for calling the initializer of the parent 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"

In any case, the true potential of super is obtained with multiple inheritance.

    
answered by 04.06.2017 в 20:54