On 18 February 2014 07:35, Greg Ewing <greg.ew...@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?

Paul
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
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