def main() :
a = Complex(3.0,-4.5)
b = Complex(4.0, -5.0)
c = Complex(-1.0, 0.5)
print(a+b)
print(a+b-c)
print(a-b)
print(a-b+c)
print(a-c)
print(b == (a-c))
class Complex:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
return Complex(self.x + other.x, self.y + other.y)
def __sub__(self, other):
return Complex(self.x - other.x, self.y - other.y)
def __str__(self):
return f"Complex({self.x}, {self.y})"
main()
I want to get the answer like this:
Complex(7.0,-9.5)
Complex(8.0,-10.0)
Complex(-1.0,0.5)
Complex(-2.0,1.0)
Complex(4.0,-5.0)
True
Everything is Okay until Complex(4.0, -5.0)
, but I got 'False
' in the end. So I tried to debug and found <__main__.Complex object at 0x0397~~~> == <__main__.Complex object at 0x03E7~~~>
(numbers after 'at' is different) so False
is shown. I tried to print(a-c)
and print(b)
each and they look same when printed but something like address is different. What should I do to get True
instead of False
?