Ran into this problem (in Python 2.7.5) with a little typo:
def foo(): return 3
if foo > 8:
launch_the_nukes()
Dang it, I accidentally exploded the Moon.
My understanding is that E > F
is equivalent to (E).__gt__(F)
and for well behaved classes (such as builtins) equivalent to (F).__lt__(E)
.
If there's no __lt__
or __gt__
operators then I think Python uses __cmp__
.
But, none of these methods work with function
objects while the <
and >
operators do work. What goes on under the hood that makes this happen?
>>> foo > 9e9
True
>>> (foo).__gt__(9e9)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'function' object has no attribute '__gt__'
>>> (9e9).__lt__(foo)
NotImplemented
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…