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

Terry Reedy tjreedy at udel.edu
Mon Feb 17 19:50:20 CET 2014

On 2/17/2014 12:59 PM, Terry Reedy wrote:
> On 2/17/2014 10:22 AM, M.-A. Lemburg wrote:
>> On 17.02.2014 15:38, Jon Ribbens wrote:
>>> On Mon, Feb 17, 2014 at 12:43:25PM +0100, M.-A. Lemburg wrote:
>>>> This doesn't only apply to numeric comparisons. In Python 2 you
>>>> can compare None with any kind of object and it always sorts first,
>>> No you can't. See http://bugs.python.org/issue1673405 .

This issue was about the fact that datetimes do not compare with None, 
which means that even in 2.x, putting None in a list of datetimes (to 
mean no datetime) means that the list cannot be sorted.

>>> According to Tim Peters, the "None is less than everything" rule
>>> never existed.
> Tim is correct. Copying from my other response (posted after you wrote
> this)
>  >>> class Bottom(object):  # get same results below without 'object'
>      def __lt__(self, other):
>          return True
> # the following two results are consistent and
> # contradict the claim that 'None is smaller than anything'
>  >>> Bottom() < None
> True
>  >>> cmp(Bottom(), None)
> -1
> # the following two results are not consistent with the
> # definition of cmp, so 1 of the 2 is buggy
>  >>> None < Bottom()
> True
>  >>> cmp(None, Bottom())
> 1
>> Well, then Tim probably didn't read the code in object.c :-)
> I did, as I suspect Time has also. Function default_3way_compare is a
> 'final fallback'. The comment within, besides being a code comment and
> not the doc, is wrong unless 'anything' is qualified.

FWIW: do_cmp first calls try_rich_to_3way_compare and if that fails it 
calls try_3way_compare and if that fails it calls default_3way_compare. 
Somewhat confusingly, try_rich_to_3way_compare first calls 
try_3way_compare and if that fails, it calls default_3way_compare. So 
the backup calls in do_cmp are redundant, as they will have already 
failed in try_rich_to_3way_compare.

The special casing of None in default_3way_compare was added by Guido in 
rev. 16123 on 2001 Jan 22. Before that, None was compared by the 
typename, as still specified in the docs.

Tim was correct when he wrote "For older types, the result of inequality 
comparison with None isn't defined by the language, and the outcome does 
vary across CPython releases."

Terry Jan Reedy

More information about the Python-Dev mailing list