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

Paul Moore p.f.moore at gmail.com
Tue Feb 18 09:10:17 CET 2014

On 18 February 2014 07:35, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> If you don't want to touch comparison in general,
> how about an option to sort()?
>   results = sorted(invoices, key=attrgetter('duedate'), none='first')

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))

Admittedly the key function is starting to get complex, but I find
that key functions often do - they are the one big area where Python
uses a lot of functional-style constructs. The existence of
itemgetter/attrgetter are a testament to this fact.

PS isn't this python-ideas territory by now?


More information about the Python-Dev mailing list