1
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?

kinshukdua
  • 1,944
  • 1
  • 5
  • 15
dkaelfkae
  • 13
  • 3
  • 2
    Your class doesn't implement `__eq__`, or any comparison methods. – jonrsharpe May 15 '21 at 17:12
  • You need to define a [`__eq__`](https://docs.python.org/3/reference/datamodel.html#object.__eq__) method (and probably other comparison dunders too) to override the default object equality check. – SuperStormer May 15 '21 at 17:13
  • `object at 0x0397` is just the memory adress, so 2 object have different adresses, that's normal. Also read about https://stackoverflow.com/questions/1436703/what-is-the-difference-between-str-and-repr to get a nice readable print for your objects – azro May 15 '21 at 17:15
  • You do know that Python has complex numbers built-in, right? e.g. `a = 3+-4.5j` – thebjorn May 15 '21 at 17:18

2 Answers2

3

As pointed out in the comments you haven't defined the __eq__ operator which is used for equality comparisons (==). Since it's not defined python doesn't know how to compare these two and instead tries comparing their identity like the is keyword, which checks if they are the same objects (the address or the "number" after the at is the same).

Here is one implementation of __eq__

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})"

    #returns True if the two objects are equal
    def __eq__(self, other):
        return self.x == other.x and self.y == other.y

main()

and you should get True as your final output

kinshukdua
  • 1,944
  • 1
  • 5
  • 15
  • Float should never be compared for equality. Use epsilon ball criterion instead. https://floating-point-gui.de/errors/comparison/ – jlandercy May 15 '21 at 17:23
  • Really thanks! It works but I got small warnings **Implementing __eq__ without also implementing __hash__** does it mean when I use __eq__, I always have to use __hash__? – dkaelfkae May 15 '21 at 17:51
0

A quick (but weak) solution would be to change the line

print(b == (a-c))

into

print(str(b) == str(a-c))

However, if you wanted to implement more foolproof equality checks you should check out the dunder method __eq__. For example, you could add the method:

def __eq__(self, other):
    return (self.x, self.y) == (other.x, other.y)
Lucas Ng
  • 671
  • 4
  • 11