4 ms·
Unfortunately, it's more permissive than mathematical notation: `3 > 1 < 2` evaluates to True.
by murderfs 2y ago
Unfortunately, it's more permissive than mathematical notation: `3 > 1 < 2` evaluates to True.
- solveit 2y agoI think mathematicians looking at that expression would curl their lips in mild disgust, but also evaluate it to True.
- throwaway313373 2y agoI 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
- Kim_Bruning 2y agoI love the fact that you defined Foo.__eq__ by means of a chaining comparison!
- jyounker 2y agoAs someone who trained in mathematics, I would 100% say that mathematicians would evaluate it as true. It's not the cleanest way to write it, but intent is clear.