0

So I discovered that functions evaluate with comparison operators. E.g.

def randomFunction() :
    return None

print randomFunction > 0 # True
print randomFunction > float('inf') #True !!!!
print randomFunction < 0 # False
print randomFunction == 0 # False

This was the cause of a subtle bug where code appeared to be working as an if statement was missing () and something which should mostly evaluate to true was always evaluating true.

Why is this? How is this even implemented when the dir(randomFunction) has no eq method?

It seems incredible that a language that doesnt allow assignment in if statements should allow comparisons between unevaluated functions and a number? I tried to force my debugger to step into it but it thinks the comparison is atomic. Is there some sensible reason behind this or is it just another WAT moment?

phil_20686
  • 4,000
  • 21
  • 38
  • It's a WAT, fixed in Python 3. This comparison behavior made a few algorithms more convenient to implement, but it just wasn't worth the confusing behavior and the errors it hid. – user2357112 Apr 07 '16 at 17:32
  • (They didn't take out the reference comparison fallback for `==`, though.) – user2357112 Apr 07 '16 at 17:38

0 Answers0