Is everything more than No?

Is there a built-in Python data type other than None for which:

 >>> not foo > None True 

where is foo value of this type? What about Python 3?

+57
python python-datamodel
Feb 06 '10 at 18:32
source share
2 answers

None always smaller than any data type in Python 2 (see object.c ).

In Python 3, this has been changed; now making comparisons on things without reasonable natural ordering in a TypeError . From 3.0 "what new" updates :

Python 3.0 simplified the rules for streamlining comparisons:

Order comparison operators ( < , <= , >= , > ) raise a TypeError exception when the operands have no meaningful natural ordering. Thus, expressions such as: 1 < '' , 0 > None or len <= len are no longer valid and, for example, None < None raises a TypeError instead of returning False . The corollary is that sorting a heterogeneous list no longer makes sense - all elements must be comparable with each other. Note that this does not apply to the == and != Operators: objects of different incomparable types are always compared unevenly with each other.

This upset some people because it was often convenient to do things like sort a list that had some None values ​​and have None values ​​grouped together at the beginning or end. There has been a thread on the mailing list in the past , but the end point is that Python 3 is trying to avoid making arbitrary ordering decisions (which was a lot in Python 2).

+70
Feb 06 '10 at 18:38
source share

From 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.

+26
Feb 06 '10 at 18:40
source share



All Articles