(Mar-12-2021, 09:57 PM)Drone4four Wrote: the same outcome can be achieved with 75% less codeActually, your code is not replicating all the functionality of the original script. e.g. in original script you can change height/weight of the instance, they print the square attributes, their code is heavily commented (which amounts for big part of these extra lines), etc. i.e. you emulate just the outcome, not 100% of the functionality/level of documenting the code. Of course, their code can be refactored - get rid of redundant things like printing
"Retrieving the height"
. All that said, I agree with others who pointed out that their code is terrible. Like @Gribouillis said - it may be better to find another course, learning material.
Others have pointed enough of deficiencies, but here are some more:
- Having a
Square
class that allows for differentheight
andwidth
is poor choice of class name, confusing and misleading, and a real math non-sense.Rectangle
is what you have right now. One may have aRectangle
class like this one, then have e.g.Square
class that inherits from it, that ensure equal sides. One may even have aPolygon
orShape
class thatRectangle
will inherit from. That is also an example of the benefit of OOP over your approach - extensibility.
height
andwidth
default values are strings and the values are expected and stored asstr
, converted every time when area is calculated. Even if they want to allow to instantiate the class by passingstr
as arguments, then they can convert inside the setter and store value stored as int, float or other numeric type. I understand they want to use str.isdigit for validation, but it will raise error if you pass int (and that is what I find way more logical to do, than passstr
).
- the dubious use of getters/setters was already mentioned, but I am ready to give them the benefit of the doubt - i.e. if they wanted to show it as an existing option (i.e. there are legit cases when it makes sense ti use the). In addition, if it was me I would make the area a property, not method
So, something like
class Rectangle: def __init__(self, height, width): self.height = float(height) # allow to take str as input, but raise error when not valid one self.width = float(width) @property def area(self): return self.width * self.height class Square(Rectangle): def __init__(self, side): super().__init__(side, side) self.side = float(side) @property def side(self): return self._side @side.setter def side(self, value): self._side = self._width = self._height = value @property def height(self): return self._height @height.setter def height(self, value): self._side = self._width = self._height = value @property def width(self): return self._width @height.setter def width(self, value): self._side = self._width = self._height = value if __name__ =='__main__': rectangle = Rectangle(3, 4.5) print(f'Height: {rectangle.height}') print(f'Width: {rectangle.width}') print(f'{rectangle.height} X {rectangle.width} rectangle has area of {rectangle.area}') square = Square('2') print(f'Height: {square.height}') print(f'Width: {square.width}') print(f'Square with side {square.side} has area of {square.area}') square.side = 5.5 print(f'Height: {square.height}') print(f'Width: {square.width}') print(f'Square with side {square.side} has area of {square.area}') square.area = 5 # this will raise AttributeError: can't set attribute
Output:Height: 3.0
Width: 4.5
3.0 X 4.5 rectangle has area of 13.5
Height: 2.0
Width: 2.0
Square with side 2.0 has area of 4.0
Height: 5.5
Width: 5.5
Square with side 5.5 has area of 30.25
Height: 6
Width: 6
Square with side 6 has area of 36
Traceback (most recent call last):
File "/home/boyan/sandbox2/forum.py", line 95, in <module>
square.area = 5 # this will raise AttributeError: can't set attribute
AttributeError: can't set attribute
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs