itemgetter with default arguments

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat May 5 05:24:25 EDT 2018


On Sat, 05 May 2018 09:33:37 +0200, Peter Otten wrote:

> I think you have established that there is no straight-forward way to
> write this as a lambda.

What I *mostly* established was that I was having a "cannot brain, I have 
the dumb" day, because the solution with ternary if was obvious in 
hindsight:

    lambda x: x[3] if len(x) > 4 else "MISSING"


nevertheless, I still think that making this a feature of itemgetter and 
attrgetter is worthwhile.


> But is adding a default to itemgetter the right conclusion?

I think so, but others (including Raymond Hettinger and Serhiy Storchaka) 
don't. Even when they're clearly and obviously wrong, as in this case 
*wink*, I don't think we should just dismiss their objection without 
careful thought.



-- 
Steve




More information about the Python-list mailing list