Is there a Python built-in datatype, besides None, for which:
>>> not foo > None True
where foo is a value of that type? How about Python 3?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
None is always less than any datatype in Python 2 (see object.c).
In Python 3, this was changed; now doing comparisons on things without a sensible natural ordering results in a TypeError. From the 3.0 “what’s new” updates:
Python 3.0 has simplified the rules for ordering comparisons:
The ordering comparison operators (
<,<=,>=,>) raise aTypeErrorexception when the operands don’t have a meaningful natural ordering. Thus, expressions like:1 < '',0 > Noneorlen <= lenare no longer valid, and e.g.None < NoneraisesTypeErrorinstead of returningFalse. A corollary is that sorting a heterogeneous list no longer makes sense – all the elements must be comparable to each other. Note that this does not apply to the==and!=operators: objects of different incomparable types always compare unequal to each other.
This upset some people since it was often handy to do things like sort a list that had some None values in it, and have the None values appear clustered together at the beginning or end. There was a thread on the mailing list about this a while back, but the ultimate point is that Python 3 tries to avoid making arbitrary decisions about ordering (which is what happened a lot in Python 2).
Method 2
From the Python 2.7.5 source (object.c):
static int
default_3way_compare(PyObject *v, PyObject *w)
{
...
/* None is smaller than anything */
if (v == Py_None)
return -1;
if (w == Py_None)
return 1;
...
}
EDIT: Added version number.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0