None < 0 ???

Arnau Sanchez pyarnau en gmail.com
Mie Ene 2 22:53:11 CET 2008


Oswaldo Hernández escribió:

> Si la definición de None es la ausencia de valor, la expresión None < 1 
> ¿no deberia ser tambien false?

Como "None" no implementa las "rich comparisons" (__gt__, __lt__, etc) 
no tiene mucho sentido compararlo con otros objetos. Al respecto la 
documentación dice:

http://docs.python.org/ref/comparisons.html

"The operators <, >, ==, >=, <=, and != compare the values of two 
objects. The objects need not have the same type. If both are numbers, 
they are converted to a common type. Otherwise, objects of different 
types always compare unequal, and are ordered consistently but arbitrarily."

Sólo se le pide ser consistente, así que el resultado depende de la 
implementación. Si miramos las fuentes, en CPython vemos:

(Objects/object.c)

/* None is smaller than anything */
         if (v == Py_None)
                 return -1;
         if (w == Py_None)
                 return 1;

Lo que explica el comportamiento que observas.
_______________________________________________
Lista de correo Python-es 
http://listas.aditel.org/listinfo/python-es
FAQ: http://listas.aditel.org/faqpyes





Más información sobre la lista de distribución Python-es