A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2014-February/132448.html below:

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

[Python-Dev] python 3 niggle: None < 1 raises TypeErrorPaul 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?

Paul
More information about the Python-Dev mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4