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

Georg Brandl g.brandl at gmx.net
Tue Feb 18 09:39:58 CET 2014


Am 18.02.2014 08:35, schrieb Greg Ewing:
> Tim Peters wrote:
>> 
>> [Greg Ewing]
>> 
>>>often
>>>one wants to sort a collection of objects having
>>>keys that can take on null values.
>> 
>> Perhaps that's often true of your code, but it's never been true of mine.
> 
> It's fairly common in accounting circles. I have a
> collection of invoices, each of which can have a due
> date specified, but doesn't have to. I want to sort
> the invoices by due date. It's not particularly
> important whether the missing dates sort first or
> last, but it *is* important that the sort *not blow
> up*.
> 
> Dates seem to be a particularly irksome case. Here's
> one suggestion from StackOverflow for dealing with
> the problem:
> 
>     import datetime
>     mindate = datetime.date(datetime.MINYEAR, 1, 1)
> 
>     def getaccountingdate(x):
>       return x['accountingdate'] or mindate
> 
>     results = sorted(data, key=getaccountingdate)
> 
> That's a ridiculous amount of boilerplate for doing
> something that ought to be straightforward.

Seeing how you need a key function in any case for this sort to work,
it's only the "or mindate" added, which I can't recognize as "ridiculous
amount of boilerplate".  Much more so since you can put the key function
in a shared module and import it from there anywhere you need it.

Georg



More information about the Python-Dev mailing list