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

Nick Coghlan ncoghlan at gmail.com
Mon Feb 17 23:38:48 CET 2014


On 18 Feb 2014 08:25, "Nick Coghlan" <ncoghlan at gmail.com> wrote:
>
>
> On 17 Feb 2014 22:25, "M.-A. Lemburg" <mal at egenix.com> wrote:
> >
> > On 17.02.2014 13:12, Nick Coghlan wrote:
> > > On 17 Feb 2014 21:15, "M.-A. Lemburg" <mal at egenix.com> wrote:
> > >>
> > >> None is special in Python and has always (and intentionally) sorted
> > >> before any other object. In data processing and elsewhere in Python
> > >> programming, it's used to signal: no value available.
> > >
> > > This is the first I've ever heard of that sorting behaviour being an
> > > intentional feature, rather than just an artefact of Python 2 allowing
> > > arbitrarily ordered comparisons between different types. Can you
point me
> > > to the relevant entry in the Python 2 language reference?
> >
> > This is not documented anywhere in the language spec, AFAIK. It
> > is documented in the code (Python 2.7; Object/object.c):
> >
> > default_3way_compare(PyObject *v, PyObject *w)
> > ...
> >     /* None is smaller than anything */
> >     if (v == Py_None)
> >         return -1;
> >     if (w == Py_None)
> >         return 1;
> >
> > Note that it's not important whether None is smaller or greater
> > than any other object. The important aspect is that it's sorting
> > order is consistent and doesn't raise a TypeError when doing an
> > ordered comparison with other objects.
>
> Thanks, that's enough to persuade me that it is a good idea to restore
that behaviour (and make it an official part of the language spec) as part
of the "eliminate remaining barriers to migration from Python 2" effort in
3.5.
>
> It will mean that SQL sorting involving NULL values maps cleanly again,
and it's easy enough to use any() to check that container doesn't contain
None.

Note that I sent this before reading Tim's reply regarding the origin of
the Python 2 sorting behaviour, and Terry's point that the relevant Python
2 code lived in the legacy 3 way comparison fallback that was deliberately
removed from 3.0.

MAL's perspective does still at least mean I am only -0 rather than -1 on
making None always sorts first rather than throwing an exception, but my
preferred solution remains to provide backporting-friendly support for
handling of None values in comparisons (and particularly sorting
operations): http://bugs.python.org/issue20630

Cheers,
Nick.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20140218/5a661cbf/attachment.html>


More information about the Python-Dev mailing list