Python Forum
class method on two object
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
class method on two object
#1
class Rectangle():
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def dRec_area(self1, self2):
        area = (self1.x * self1.y) + (self2.x * self2.y) 
        return area
    
x1 = input("Please insert the width: ")
y1 = input("Please insert the length: ")
rectangle1 = Rectangle(x1, y1)

x2 = input("Please insert the width: ")
y2 = input("Please insert the length: ")
rectangle2 = Rectangle(x2, y2)

print(rectangle1.rectangle2.dRec_area())
How to apply the method on two objects?
Reply
#2
print(Rectangle.dRec_area(rectangle1,rectangle2))
Reply
#3
TypeError: can't multiply sequence by non-int of type 'str'

why does this error appear after I run the program?

Thanks for the help
Reply
#4
Input entered is of type string. You got that error since you have not converted to integer before you perform arithmetic calculation

x1 = int(input("Please insert the width: "))
Reply
#5
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))
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pass an object to a class, then make an object of it and pass again TomasAm 11 4,515 Nov-09-2020, 04:47 PM
Last Post: buran
  Please, how do I call the method inside this class Emekadavid 1 1,630 Jun-26-2020, 01:26 PM
Last Post: Yoriz
  Class/Method Confusion ramadan125 1 2,592 Sep-10-2018, 12:19 AM
Last Post: ichabod801
  Class and calling a method Zatoichi 3 3,091 Mar-13-2018, 08:44 PM
Last Post: Zatoichi

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020