[Python-Dev] itemgetter/attrgetter extension
Nick Coghlan
ncoghlan at iinet.net.au
Thu Mar 10 12:19:16 CET 2005
Raymond Hettinger wrote:
> Any objections to extending itemgetter() and attrgetter() to be able to
> extract multiple fields at a time?
>
> # SELECT name, rank, serialnum FROM soldierdata
> map(attrgetter('name', 'rank', 'serialnum'), soldierdata)
>
> # SELECT * FROM soldierdata ORDER BY unit, rank, proficiency
> sorted(soldierdata, key=attrgetter('unit', 'rank', 'proficiency'))
Breaking the symmetry with getattr() bothers me a little, as does the signature
change that occurs when multiple arguments are given (returning a tuple, whereas
the single argument returns just the retrieved attribute).
For itemgetter, I'd like to see multiple arguments eventually map to
multi-dimensional slices (to preserve symmetry with indexing syntax).
Call it -1 for itemgetter and -0 for attrgetter.
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at email.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
More information about the Python-Dev
mailing list