When does Python 2 consider one function "greater than" or "less than" another function? -


this question has answer here:

i discovered it's legal in python compare arbitrary functions using operators >, <, >= , <=. seems bit silly; half expected such comparison false (or throw exception), docs say: "most other objects of built-in types compare unequal unless same object; choice whether 1 object considered smaller or larger 1 made arbitrarily consistently within 1 execution of program."

so did little experimenting, implied perhaps order in functions defined 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 source code (noting here i'm way out of depth @ point, having of 2 months' experience c). this answer led me python/ceval.c, seems handle these comparison operators using pyobject_richcompare() (line 4640). couldn't find definition function, a pep, , that's i'm stuck.

how might predict result of these seemingly nonsensical operations? (and long we're here... why 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 > > > abs > __import__ > true 

in python2, these type of comparisons done based on value of id() of object:

in [1]: def g():    ...:     pass  in [2]: def y():    ...:     pass  in [3]: g > y out[3]: true  in [4]: id(g) out[4]: 55898312  in [5]: id(y) out[5]: 54420736 

the value of id() depends on memory address of function object, might depend on arbitrary things previous history of garbage collector. reason, python's developers removed functionality, comparing functions in python3 gives error:

in [3]: g > y --------------------------------------------------------------------------- typeerror                                 traceback (most recent call last) /home/xxx/<ipython-input-3-9ebc8ff65838> in <module>() ----> 1 g > y  typeerror: unorderable types: function() > function() 

in python 3, comparison equality still legal, since not depend on arbitrary value of id():

in [4]: g == y out[4]: false  in [5]: g != y out[5]: true 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -