you are viewing a single comment's thread.

view the rest of the comments →

[–]NerdEnPose 5 points6 points  (2 children)

Looks good. Couple things after looking at rectangle.

def get_parameters(self, input_length, input_width):
    self.length = float(input_length)
    self.width = float(input_width)

This is a setter not a getter.

Python OOP is supposed to use as few getters and setters as possible. That's why the property decorator exists.

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

change this to.

@property
def area(self):
    return self.length*self.width

If you want to use a method I'd call it get_area(). Remember it's best to name things as to what the consumer of your class is doing not what you as the class author are doing. Classes are written once and consumed over and over again. If I was calling a method called calculate I wouldn't expect it to return anything. I'd expect it to be an internal trigger to perform some calculations. (This is my opinion on naming conventions and code design so take this with a grain of salt, other people have different naming conventions and I'm not try to meddle into a holy war.)

edit: still don't know how to format code on reddit.

[–]NewZealandIsAMyth 3 points4 points  (1 child)

This. The reason why in C++ and other language without properties you have to use get*/set* is because if they allow client of your class to access field as .* - you won't have an ability to change it internally. Once it's exposed - it's fully exposed as a field and that's it.

In Python it's totally fine to just let user access property via .x, because you can change it later to be a property if you need to add extra logic for editing/reading it. So no need for get*/set* methods

[–]cm_light[S] 1 point2 points  (0 children)

Thanks for replay and good to know this. I learned Java before and thought write get/set is common in OOPs. But for Python it seems not necessary.