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

2014年2月18日 06:30:57 -0800

On 2014年02月18日 14:11, MRAB wrote:
On 2014年02月18日 13:48, Serhiy Storchaka wrote:
18.02.14 10:10, Paul Moore написав(ла):
Or alternatively, a "default on None" function - Oracle SQL calls this
nvl, so I will too:
def nvl(x, dflt):
 return dflt if x is None else x
results = sorted(invoices, key=lambda x: nvl(x.duedate, datetime(MINYEAR,1,1))
Or, as was proposed above:
results = sorted(invoices,
 key=lambda x: (x.duedate is not None, x.duedate))
That makes me wonder.
Why is:
 None < None
unorderable and not False but:
 (None, ) < (None, )
orderable?
tuple's rich comparison uses PyObject_RichCompareBool(x, y, Py_EQ) to find the first pair of items that is unequal. Then it will test the order of any remaining elements.
 http://hg.python.org/cpython/file/79e5bb0d9b8e/Objects/tupleobject.c#l591
PyObject_RichCompareBool(x, y, Py_EQ) treats identical objects as equal.
 http://hg.python.org/cpython/file/79e5bb0d9b8e/Objects/object.c#l716
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
 -- Umberto Eco
_______________________________________________
Python-Dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to