A class is usually bigger, has more methods and by convention we use self, other for instances and cls for classes.
By convention camel case should not used in methods. (The functions attached to the class, are methods).
Instead of doing the operation in one step, you can split it up.
First step: Get area of both rectangles
Second step: Add the area
A @property attached as decorator to a method of the class, just return the value without calling it.
class Rectangle():
"""
What does this class?
"""
def __init__(self, x, y):
"""
Here is how the object is initilized...
"""
self.x = x
self.y = y
@property
def area(self):
"""
Area of rectangle
"""
return self.x * self.y
def __add__(self, other):
"""
Add area of rectangle to area of other rectangle
"""
return self.area + other.area
def __sub__(self, other):
"""
Substract area of rectangle to area of other rectangle
"""
return self.area - other.area
def __mul__(self, scalar):
"""
Multiply x and y of the rectangle by sclar and return a new.
"""
return self.__class__(self.x * scalar, self.y * scalar)
def __truediv__(self, scalar):
"""
Divide x and y of the rectangle by scalar and return a new.
"""
return self.__class__(self.x / scalar, self.y / scalar)
def __repr__(self):
"""
The representation of your object in the repl
"""
cls_name = self.__class__.__name__
return f"{cls_name}(x={self.x}, y={self.y})"
x1 = input("Please insert the width: ")
y1 = input("Please insert the length: ")
x1 = float(x1)
y1 = float(y1)
rectangle1 = Rectangle(x1, y1)
x2 = input("Please insert the width: ")
y2 = input("Please insert the length: ")
x2 = float(x2)
y2 = float(y2)
rectangle2 = Rectangle(x2, y2)
print(rectangle1 + rectangle2)
The different solution without property:
class Rectangle():
"""
What does this class?
"""
def __init__(self, x, y):
"""
Here is how the object is initilized...
"""
self.x = x
self.y = y
def drec_area(self, other):
area = (self.x * self.y) + (other.x * other.y)
return area
Rectangle(2, 1).drec_area(Rectangle(3, 1))