I just discovered that it's legal in Python to compare arbitrary functions using the operators >
, <
, >=
and <=
. This seems a bit silly; I half expected such a comparison to always be False
(or throw an exception), but the docs say: "Most other objects of built-in types compare unequal unless they are the same object; the choice whether one object is considered smaller or larger than another one is made arbitrarily but consistently within one execution of a program."
So I did a little experimenting, which implied that perhaps the order in which functions are defined is significant here:
>>> def g():
pass
>>> def y():
pass
>>> g > y
False
>>> y > g
True
>>> def r():
pass
>>> g > r
False
>>> r > g
True
>>> y > r
False
>>> r > y
True
>>> def barfoo():
pass
>>> barfoo > r > y > g
True
I tried tracking down the source code (noting here that I'm way out of my depth at this point, having all of two months' experience with C). This answer led me to Python/ceval.c, which seems to handle these comparison operators using PyObject_RichCompare()
(line 4640). I couldn't find a definition for that function, only a PEP, and that's where I'm stuck.
How might I predict the result of these seemingly nonsensical operations? (And as long as we're here... why would I want to?)
Bonus:
>>> unicode > super > object > type > tuple > str > basestring > slice > frozenset > set > xrange > memoryview > long > list > int > staticmethod > classmethod > float > file > reversed > enumerate > dict > property > complex > bytearray > buffer > bool > zip > vars > unichr > sum > sorted > setattr > round > repr > reload > reduce > raw_input > range > pow > ord > open > oct > next > min > max > map > locals > len > iter > issubclass > isinstance > intern > input > id > hex > hash > hasattr > globals > getattr > format > filter > execfile > eval > divmod > dir > delattr > compile > coerce > cmp > chr > callable > bin > apply > any > all > abs > __import__ > help
True