comparison with None

Terry Reedy tjreedy at udel.edu
Thu Apr 19 20:59:54 CEST 2007


| Alan Isaac wrote:
| > "Terry Reedy" <tjreedy at udel.edu> wrote in message
| > news:mailman.6686.1176934558.32031.python-list at python.org...
| >
| >> Should be in the reference manual section on comparisons.

| > Only to this extent:
| > http://www.python.org/doc/2.4/ref/comparisons.html
| >
| >         objects of different types always compare unequal, and are 
ordered
| >         consistently but arbitrarily.

This is the sentence I was referring to.  The key word is 'arbitrary', 
meaning withoug any particular reason why, as in "why does None compare as 
it does?"

| >         (This unusual definition of comparison was used to simplify the
| >         definition of operations like sorting

That was broken when complex numbers were added.

| >         In the future, the comparison rules for objects of different 
types are
| >         likely to change.)

Will change in 3.0.

| >         ...  Most other types compare unequal unless they are the same 
object;
| >         the choice whether one object is considered smaller or larger 
than
| >         another one is made arbitrarily but consistently within one 
execution
| >         of a program.
| >
| > This does not provide a direct answer to "why" None comparisons.

Yes it does.  There is no 'why'!

| > (As far as I can tell, None is less than any object.)

This is an artifact of the current CPython implementation.

| > However, Gary Herron's explanation makes sense: this provides a stable
| > sort when None is involved, and meets the criterion that objects of
| > different types must always compare unequal.  However this would also
| > be true if None always compared greater than any object, and the 
current
| > behavior does not seem to be guaranteed.

>From the viewpoint of the language, the current behavior is an arbitrary 
artifact.

But here, I suspect, is the implementation why.  CPython compares mixed 
objects by type object address.  It is *currently* written and compiled so 
that NoneType has the lowest address among type objects.

Terry Jan Reedy






More information about the Python-list mailing list