top | item 42039102

(no title)

throwaway313373 | 1 year ago

I can think of some more interesting examples when it violates intuition gained from mathematical notation.

When looking at `a == b == c` we naturally assume that not only `a == b` and `b == c` but also `a == c` because equality is assumed to be transitive.

The problem is that in Python we can make `==` operator to do literally whatever we want, so in Python `==` doesn't have to be transitive.

You may argue that no reasonable person would define equality in such way that is not transitive.

To which I would reply that approximate comparison of floating point numbers looks perfectly reasonable to me.

    EPSILON = 10**-9

    class Foo:
        def __eq__(self, other):
            return other.bar - EPSILON <= self.bar <= other.bar + EPSILON

discuss

order

Kim_Bruning|1 year ago

I love the fact that you defined Foo.__eq__ by means of a chaining comparison!