C
Before I give you the answer, I will tell you ways to make your code more "Pythonico", that is, as it is usually written in Python and also make some clarifications:In Python, unlike other languages such as C++, for example, there are no private methods. All methods are public, putting a low bar (not two), it is the way to indicate the programmer, which is a method is private and should not touch that.The Getters and Setters are not usually used as in other languages, but rather that class attributes are consulted and modified in a public way.An unwritten rule in Python is that you should write the least amount of code whenever possibleOnce the above has been said, it must be said that "making python" we can create private classes (it is not recommended, unless absolutely necessary), and that you can also create getters and setters with the decorator @property.Once clarified, all this, we go with your exercise, to which I have made some modifications, applying the above. In this case, I have kept everything in the same .py ; but you can make the imports as you've been doing, no problems#CLASE PADRE FIGURA GEOMÉTRICA
#____________________________________________________________________
class FiguraGeometrica:
def __init__(self, ancho, alto):
self.ancho=ancho
self.alto=alto
def __str__(self):
return "Ancho: "+ str(self.ancho)+", Alto: "+str(self.alto)+", Area = "+str(self.ancho*self.alto)
#CLASE PADRE FIGURA COLOR
#____________________________________________________________________
class Color:
def init(self, color):
self.color=color
def __str__(self):
return "Color: " + str(self.color)
#CLASE HIJA CUADRADO: aquí es donde están los errores
#____________________________________________________________________
class Cuadrado(FiguraGeometrica, Color):
#instanciamos totas las propiedades necesarias para las distintas clases
def __init__(self, ancho, alto, color):
super().__init__(ancho, alto)
super(FiguraGeometrica, self).__init__(color)
#Ponemos en el super, FiguraGeométrica en vez de Color
def __str__(self):
return super().__str__() + ", " + super(FiguraGeometrica, self).__str__()
cuadrado = Cuadrado(4, 4, "Rojo")
print(cuadrado)
What I've done:I've removed the double bass bars. since the double bars are for special methods.I have removed the getters and the setters as they are not necessary for this case of use. You can consult the variables like this: print(cuadrado.color) and modify the color cuadrado.color = "verde"I added "Area" to the special method str of FiguraGeometrica for better visualizationYour problem.Once this long introduction has been made, let's go with your problem. Everything lies in your square daughter class.Your instances, a "sided" attribute that does not exist, you have to instance your attributes "high" and "width"Since you use the super method in str Why not use it too init ♪ Though you're the way to do it is totally correct, and you see in many cases the function super() I would allow us to injection of units (dependency injection). This allows us to introduce a class between father and son, for example here it might be interesting to introduce, another class between FiguraGeometricaa and Cuadrado for example CuatroLados since the area of the rest of the figures that do not have 4 sides, its formula is differentFinally, the special method str: super() giving a definition of walking home what it means is: "after the next begins." For example when I use it in init at first I use it with my own init What I'm telling python is "Start the Square Class Builder and Then start the next builder after the Class Square, in this case is the builder of FigureGeometrica and finally, Then start the next builder who is after FigureGeometrica, in this case Color".In the method str It's the same. You can't put super(Color) Why are you telling Python to find the next class Color and there is no other class, since the two classes that are inherited are Figurageometrica and Color. So you have to put FiguraGeometrica to bring you Color.Getters and Setters.As I have told you, the Getters and Setters are not usually used in Python, unless absolutely necessary, for example in data validation, descriptors, etc. In case you want to, you can use the decorator @property.#CLASE PADRE FIGURA GEOMÉTRICA
#__________________________________________________________________
class FiguraGeometrica:
def init(self, ancho, alto):
self._ancho=ancho
self._alto=alto
@property
def ancho(self):
return self._ancho
@ancho.setter
def ancho(self, value):
self._ancho = value
@ancho.deleter
def ancho(self):
del self._ancho
@property
def alto(self):
return self._alto
@alto.setter
def alto(self, value):
self._alto = value
@alto.deleter
def alto(self):
del self._alto
def __str__(self):
return "Ancho: "+ str(self._ancho)+", Alto: "+str(self._alto)+", Area = "+str(self._ancho*self._alto)
#CLASE PADRE FIGURA COLOR
#____________________________________________________________________
class Color:
def init(self, color):
self._color=color
@property
def color(self):
return self._color
@color.setter
def color(self, value):
self._color = value
@color.deleter
def color(self, value):
del self._color
def __str__(self):
return "Color: " + str(self._color)
#CLASE HIJA CUADRADO:
#____________________________________________________________________
class Cuadrado(FiguraGeometrica, Color):
def __init__(self, ancho, alto, color):
super().__init__(ancho, alto)
super(FiguraGeometrica, self).__init__(color)
def __str__(self):
return super().__str__() + ", " + super(FiguraGeometrica, self).__str__()
cuadrado = Cuadrado(4, 4, "Rojo")
print(cuadrado)
cuadrado.color = 'Verde'
print(cuadrado.color)
print(cuadrado.alto)
As you can see here we access the attribute _color through .color. Although as I said earlier in Python all methods and attributes are public, so you can still access through _color.