Is the number less than negative infinity in python?

This is possible in python2:

None < float('-inf')

      

Also, it always returns

True

      

However, in python3, this throws

TypeError: unorderable types: NoneType() < int()

      

Why is None

comparing to integers / floats with python2? Are there any benefits or applications None

to order in python2?

+3


source to share


1 answer


First of all, Python 2 allows you to compare all types of mixed types. This wart was fixed in Python 3 eventually.

CPython implementation details : objects of different types, except for numbers, are ordered by their type names; objects of the same type that do not support correct comparison are ordered by their address.



For None

a, a quick solution was made by Guido and Tim Peters and it led to this commit in Python 2.1 (emphasis mine):

Part of the fix that removed some cases of "comparing objects of different types by comparing strings of type names". Guido and I were both in the office at the time and elsewhere: "What about None

? Comparing this to other types by comparing a string 'None'

doesn't make sense." "I, okay ... how about changing None

to - default - comparing" less "objects of other types? " "I don't see why not - of course." "Well done!

Not even 2 minutes of thought passed. There was no intention for satisfying any real-world use case here - the only intention was to pick some arbitrarily-inconsistent rule that didn't suck as badly as pretending none of them were a string "No"; -)

+7


source







All Articles