[Python-Dev] PEP 309: Partial method application

"Martin v. Löwis" martin at v.loewis.de
Fri Aug 19 07:59:38 CEST 2005

Steven Bethard wrote:
>>I thought that:
>>  operator.attrgetter() was for obj.attr
>>  operator.itemgetter() was for obj[integer_index]
> My point exactly.  If we're sticking to the same style, I would expect that for
>     obj.method(*args, **kwargs)
> we would have something like:
>     operator.methodcaller('method', *args, **kwargs)

You might be missing one aspect of attrgetter, though. I can have

  f = operator.attrgetter('name', 'age')

and then f(person) gives me (person.name, person.age). Likewise for
itemgetter(1,2,3). Extending this to methodcaller is not natural;
you would have


and then


(I know this is not the typical open/read/close pattern, where you
 would normally call read on what open returns)

It might be that there is no use case for a multi-call methodgetter;
I just point out that a single-call methodgetter would *not* be
in the same style as attrgetter and itemgetter.

>     attrget.attr   (for obj.attr)
>     itemget[key]   (for obj[key])

I agree that would be consistent. These also wouldn't allow to get
multiple items and indices. I don't know what the common use for
attrgetter is: one or more attributes?


More information about the Python-Dev mailing list