I have created 3 classes, the GeometricShape class, the Rectangle class, and the Ellipse class. The attributes of these classes have been modified to be private. I want to focus on having the rectangle class inherit from the GeometricShape class. I assume my work here will reflect on the Ellipse later.
The project advises me not to define the methods from GeometricShape into Rectangle.
I put GeometricShape in the parenthesis of the Rectangle class.I put a call base class constructor in the class as well. I am getting this message, NameError: name 'name' is not defined
.When leaving out the call base class constructor, I get this message, AttributeError: 'Rectangle' object has no attribute '_GeometricShape__name'
class GeometricShape: def __init__(self, name): #self.name = name #code before modification self.set_name(name) def get_name(self): return self.__name def set_name(self, name): self.__name = nameclass Rectangle(GeometricShape): def __init__(self, length, width): GeometricShape.__init__(self,name) #trying to get inherited attributes #self.length = length #code before modification #self.width = width #code before modification self.set_length(length) self.set_width(width) def get_length(self): return self.__length def get_width(self): return self.__width def set_length(self, length): self.__length = length def set_width(self, width): self.__width = width def get_perimeter(self): #return (2 * self.length) + (2 * self.width) #code before modification return (2 * self.__length) + (2 * self.__width) def get_area(self): #return self.length * self.width #code before modification return self.__length * self.__width
I am trying to work this out incrementally. This is the expected behavior (given by the project) I am trying to follow:
>>> Rectangle.__bases__(<class '__main__.GeometricShape'>,)>>> rectangle = Rectangle(10, 20)>>> rectangle.get_name()'Rectangle'>>> rectangle._GeometricShape__name'Rectangle'>>> rectangle.set_name('Square')>>> rectangle.get_name()'Square'>>> rectangle._GeometricShape__name'Square'
I am verifying this line by line. I got stuck where it says:
>>> rectangle = Rectangle(10, 20)>>> rectangle.get_name()
The first of these lines gives me the NameError, when I have the class base constructor in the code The second gives me the AttributeError
when I leave out the class base constructor.