[Python-ideas] Optional kwarg making attrgetter & itemgetter always return a tuple

Nick Coghlan ncoghlan at gmail.com
Sat Sep 15 14:43:59 CEST 2012

On Fri, Sep 14, 2012 at 9:36 PM, Masklinn <masklinn at masklinn.net> wrote:
> On 2012-09-14, at 13:01 , Nick Coghlan wrote:
>> Both attrgetter and itemgetter are really designed to be called with
>> *literal* arguments, not via *args. In particular, they are designed
>> to be useful as arguments bound to a "key" parameter, where the object
>> vs singleton tuple distinction doesn't matter.
> It was my understanding that they are also designed to be useful for
> mapping (such a usage is shown in itemgetter's examples), which is
> a superset of the use case outlined here.

The "key" style usage was definitely the primary motivator, which is
why the ambiguity in the *args case wasn't noticed. If it *had* been
noticed, the multiple argument support likely never would have been

As it is, the *only* case where the ambiguity causes problems is when
you want to use *args with these functions. Since they weren't built
with that style of usage in mind, they don't handle it well. Making
them even *more* complicated to work around an earlier design mistake
doesn't seem like a good idea.


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-ideas mailing list