Home > other >  How to use multiple comparison operators at once
How to use multiple comparison operators at once

Time:06-06

class Fraction:

def __init__(self, top, bottom):
    self.top = top
    self.bottom = bottom

def __repr__(self):
    return f"{self.top}/{self.bottom}"

def __ne__(self, other):
    ne_first_top = self.top * other.bottom
    ne_second_top = self.bottom * other.top
    return ne_first_top != ne_second_top

def __eq__(self, other):

    first_top = self.top * other.bottom
    second_top = other.top * self.bottom

    return first_top == second_top

f1 = Fraction(2, 3)
f3 = Fraction(1, 4)
assert f1 != f3 == True

When I run this code, I get the error AttributeError: 'bool' object has no attribute 'bottom'. Can I run this code without changing last line?

CodePudding user response:

The last line is incorrect because f3 == True is being evaluated first. It'd work if you did:

assert (f1 != f3) == True

or more simply (because True == True is always True):

assert f1 != f3

Technically you could make it "work" by forcing f3 == True to return f3, so that f1 != (f3 == True) will actually do what you want it to do:

    def __eq__(self, other):
        if other == True:
            return self

but don't do that. It would be extremely silly.

Note that since you've defined __eq__, you don't need to explicitly define __ne__ as its opposite. If __ne__ isn't explicitly defined it will just automatically be interpreted as "not __eq__"; Python doesn't generally force you to do extra work if it can at all be avoided.

CodePudding user response:

If you want to compare objects of different classes, you could amend your eq() method to handle it gracefully. At its simplest, you can do:

def __eq__(self, other):

    if type(self) == type(other):
        first_top = self.top * other.bottom
        second_top = other.top * self.bottom
        return first_top == second_top
    else:
        return False

Of course, if you want Fraction(1,1) == True to return True, then you'll need to elaborate your __eq__() method a bit further.

  • Related