0

I have to make a program where I use hierarchy using classes in Python. I have been trying to figure out why my child class Rectangle can't get the values from the parent class Quadrilateral. When I try to

print("Width:",r.get_width())
print("Area:", r.get_area())
print("Perimeter:",r.get_perimeter())

it either says I am missing parameters or it has no attribute '_Quadrilateral__width'

Can anyone explain why this is happening? Thanks

Parent

class Quadrilateral(Polygon):
    def __init__(self, width, height):
        super().__init__(width, height)

    def get_width(self):
        """ Returns width"""
        return self.__width

    def get_height(self):
        """Returns height"""
        return self.__height

    def set_width(self, width):
        """Sets the width"""
        if width <= 0:
            raise ValueError('Width must be positive.')
        self.__width = width

    def set_heigth(self, height):
        """Sets the height"""
        if height <= 0:
            raise ValueError('Height must be positive')
        self.__height = height

Child

try: Quadrilateral
except: from Quadrilateral import Quadrilateral

class Rectangle(Quadrilateral):
    def __init__(self, width, height):
        super().__init__(width, height)

    def get_area(self, width, height):
        """returns the area of a Rectangle"""
        return self.__width * self.__height

    def get_perimeter(self, width, height):
        """returns the perimeter of a Rectangle"""
        return 2 * (self.__width + self.__height)

if __name__ == '__main__':
    r = Rectangle(2,4)
    print("Width:",r.get_width())
    print("Area:", r.get_area())
    print("Perimeter:",r.get_perimeter())

I want to make a rectangle with width of 2 and length of 4 to test it but it won't let me. I have a higher class. Which might be a problem

class Polygon:
"""Polygon Parent class"""
    def __init__(self, width, height):
        self.__width = width
        self.__height = height

I want to have the Polygon class have all my function headers but its giving me syntax errors. Any chance I can have them there without having to write out all of what each function does?

AChampion
  • 29,683
  • 4
  • 59
  • 75
Luis
  • 21
  • 5
  • Why do you want the functions defined in base if there are no sensible implementations? If you want to force subclasses to implement them, i.e. abstract method then take a look at https://docs.python.org/3/library/abc.html – AChampion Sep 15 '15 at 04:39
  • Well I have to have a Polygon class, for this project, yet I don't know what to have in there except all my functions there. The number of sides might be useful to determine what type of polygon it is but I don't want to make the user have to give how many sides plus all the other values – Luis Sep 15 '15 at 04:47
  • 1
    Don't use leading double underscores for your names, this marks them as private, so you can't refer to them in subclasses, change to a single underscore. You can always define the function with a `pass` as the implementation or raise an exception. – AChampion Sep 15 '15 at 04:52
  • I have to make all data members private, so I can only change stuff through setters. Does that make my syntax different? Like self.__width to self._get_width ? – Luis Sep 15 '15 at 05:01
  • See your `__init__` calls for syntax of calling overloaded base class methods. – AChampion Sep 15 '15 at 05:08
  • Variable names with double leading underscores use name mangling when run in a class. This generally means that the variable can only be accessed from that exact class (not even from its subclasses). Generally in Python you don't want this. It's much nicer to have "private" variables with just a single leading underscore, which marks them (to a Python programmer) as being intended to be private, but without changing their behavior in the code (relative to normal variables). I'd also suggest that using lots of getters and setters is un-pythonic, but it sounds like that's not your design. – Blckknght Sep 15 '15 at 05:41
  • Even when I use the single underscore. I still get an error saying I need 2 parameters in my r.get _area() function. Shouldn't my rectangle be created with a width and height already? why is it not passing the values? – Luis Sep 15 '15 at 05:44
  • Note that: 1. If your subclass implementation of a method literally just passes all of its parameters up to the superclass, *you can leave it out entirely*; and 2. You should use `@property` not `get_` and `set_` methods, which would also easily allow validation on initialisation. Without an [mcve] (*what errors?*) it's hard to solve your specific problem, but a method body can't be empty - `pass` or `raise NotImplementedError` instead. – jonrsharpe Sep 15 '15 at 07:48

0 Answers0