4

This is possible in python2:

None < float('-inf')

Also, it always returns

True

However, on python3, this throws

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

Why is None comparable to integers/floats with python2? Are there any benefits or applications to None being orderable in python2?

jsbueno
  • 99,910
  • 10
  • 151
  • 209
cs95
  • 379,657
  • 97
  • 704
  • 746
  • Seems to be by design, at least. Not sure why it's useful though: https://hg.python.org/cpython/file/ab05e7dd2788/Objects/object.c#l778 – neophlegm Aug 02 '17 at 00:48
  • @AshwiniChaudhary I don't think it's a duplicate. None is always smaller than any integer regardless of their reference. – cs95 Aug 02 '17 at 00:51
  • @AshwiniChaudhary Okay, thanks, that'll do it. – cs95 Aug 02 '17 at 00:52
  • Everything was comparable in Python 2. Objects of different types were guaranteed to be compared consistently, but the standard made no guarantee. CPython 2 implementation detail:"Objects of different types except numbers are ordered by their type names; objects of the same types that don’t support proper comparison are ordered by their address." – juanpa.arrivillaga Aug 02 '17 at 00:53
  • @juanpa.arrivillaga The dupes are fine but one question of mine they don't answer is how one can take advantage of the orderability of `None` to create succinct code. That was honestly the main purpose of asking this question - to find out how one can apply this. Maybe it's a bit broad. – cs95 Aug 02 '17 at 00:56
  • The [relevant docs](https://docs.python.org/2/library/stdtypes.html#comparisons). – juanpa.arrivillaga Aug 02 '17 at 00:57
  • [For older types, the result of inequality comparison with None isn't defined by the language, and the outcome does vary across CPython releases.](http://bugs.python.org/issue1673405) – Ashwini Chaudhary Aug 02 '17 at 01:04
  • @AshwiniChaudhary Please reopen and post as an answer :p – cs95 Aug 02 '17 at 01:27

1 Answers1

8

First of all Python 2 allowed comparing all types of mixed types. This wart was fixed in Python 3 eventually.

CPython implementation detail: Objects of different types except numbers are ordered by their type names; objects of the same types that don’t support proper comparison are ordered by their address.

For None a quick decision was made by Guido and Tim Peters and it resulted in this commit in Python 2.1(emphasis mine):

Part of fixing that was removing some cases of "compare objects of different types by comparing the type name strings". Guido & I were both in the office at the time, and one said to the other: "what about None? Comparing that to other types via comparing the string 'None' doesn't make much sense." "Ya, OK ... how about changing None to - by default - comparing 'less than' objects of other types?" "Don't see why not - sure." "OK, done!

No more than 2 minutes of thought went into it. There was no intent to cater to any real use case here - the only intent was to pick some arbitrary-but-consistent rule that didn't suck quite as badly as pretending None was the string "None" ;-)

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504