[Python-Dev] python 3 niggle: None < 1 raises TypeError

Tim Peters tim.peters at gmail.com
Mon Feb 17 21:12:47 CET 2014

The behavior of None in comparisons is intentional in Python 3.  You
can agitate to change it, but it will probably die when Guido gets
wind of it ;-)

The catch-all mixed-type comparison rules in Pythons 1 and 2 were only
intended to be "arbitrary but consistent".  Of course each specific
release picked some specific scheme, but these schemes weren't
documented, and intentionally not.

For "a long time" CPython's default for mixed-typed comparisons was to
compare the names of the types (as strings).  And for just as long,
nobody had noticed that this _wasn't_ always "consistent".

For example, 3L < 4 must be true.  But [] < 3L was also true (because
"list" < "long"), and 4 < [] was true too (because "int" < "list").
So, in all,

    [] < 3L < 4 < []

was true, implying [] < [].

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" ;-)

Guido wanted to drop all the "arbitrary but consistent" mixed-type
comparison crud for Python 3. Nothing special about None in that.  As
already noted, the various `datetime` types were the first to
experiment with implementing full blown Python3-ish mixed-type
comparison rules.  After the first two times that caught actual bugs
in code using the new types, there was no turning back.  It's not so
much that Python 3 finished the job as that Python 2 started it ;-)

much-ado-about-nonething-ly y'rs  - tim

More information about the Python-Dev mailing list