python – Creating a Rectangle class

python – Creating a Rectangle class

Python does not restrict the access to private data attributes, so you seldom get yourself writing getters and setters like in more restrictive languages (we are all consenting adults).

Except when it is something for internal use (an implementation detail that you may change in the future) you just expose the property to the world – so a more idiomatic Rectangle would be just this:

class Rectangle(object):
    def __init__(self, width=1, height=1):
        self.width = width
        self.height = height
    def area(self):
        return self.width * self.height


>>> r = Rectangle(5, 10)
>>> r.area
>>> r.width = 100
>>> r.area

Of course you can write the Rectancle class using getters and setters, but you only do that when you want to validate or transform the input – then you probably want to learn more about the @property decorator.

Youve got a few issues with your class. See the below comments

class Rectangle:
    # Init function
    def __init__(self):
        # The only members are length and width
        self.length = 1
        self.width = 1

    # Setters
    def set_width(self, width):
        self.width = width

    def set_length(self, length):
        self.length = length

    # Getters
    def get_width(self):
        return self.width

    def get_length(self):
        return self.length

    def get_area(self):
        return self.length * self.width

    # String representation
    def __str__(self):
        return length = {}, width = {}.format(self.length, self.width)

Testing the class

>>> a = Rectangle()
>>> a.set_width(3)
>>> a.set_length(5)
>>> a.get_width()
>>> a.get_length()
>>> a.get_area()
>>> print(a)
length = 5, width = 3

As others have noted, setters and getters are superfluous in Python, as all member variables are public. I understand that these methods are required for your assignment, but in the future, know that you can save yourself the trouble and just directly access the members

>>> a.length       # Instead of the getter
>>> a.length = 2   # Instead of the setter
>>> a.length

python – Creating a Rectangle class

First, this assignment is a very bad idea. In Python, you almost never want private attributes and getter and setter functions, and whoevers teaching you to do this is leading you astray.

But, if you just want to pass the assignment instead of learning how to write decent Python code, heres how you do it.

First, to create an attribute named __length, you just assign to it, the same as any other attribute:

def __init__(self):
    self.__length = 1

Now, to write getters and setters for that attribute, do the same thing:

def get_length(self):
    return self.__length

def set_length(self, length):
    self.__length = length

Now, get_area is a bit trickier, because you dont have an __area to get. (This is a stupid idea, because it looks like a getter function even though it isnt…) But you know how to figure out the area of a rectangle: its just the length times the width, right?

def get_area(self):
    return self.__length * self.__width

The __str__ method is the only good idea in the whole assignment—although its probably a bad idea to write a __str__ without a __repr__ for a class like this. Anyway, both of these are methods that just return a string with some useful representation of your objects. The str should be something friendly to an end-user, while the repr should be something useful to the programmer (you, or someone using your class). For example:

def __str__(self):
    return {} x {} rectangle.format(self.__length, self.__width)

def __repr__(self):
    return {}({}, {}).format(type(self).__name__, self.__length, self.__width)

Leave a Reply

Your email address will not be published.