[Python-Dev] operator.attrgetter(attr[, args...]) etc.
Oscar Benjamin
oscar.j.benjamin at gmail.com
Wed Nov 21 15:49:05 CET 2012
On 21 November 2012 03:57, Leo <sdl.web at gmail.com> wrote:
> Sorry the python issue tracker seems broken (I cannot log in). So I am
> posting it here.
>
> In the doc:
>
> operator.attrgetter(attr[, args...])
> operator.itemgetter(item[, args...])
> operator.methodcaller(name[, args...])
>
> The signatures of these functions seem confusing. ARGS is not documented
> and does not match the python implementation in the doc.
What documentation are you reading?
$ python
Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import operator
>>> help(operator.attrgetter)
Help on class attrgetter in module operator:
class attrgetter(__builtin__.object)
| attrgetter(attr, ...) --> attrgetter object
|
| Return a callable object that fetches the given attribute(s) from
its operand.
| After, f=attrgetter('name'), the call f(r) returns r.name.
| After, g=attrgetter('name', 'date'), the call g(r) returns (r.name, r.date).
| After, h=attrgetter('name.first', 'name.last'), the call h(r) returns
| (r.name.first, r.name.last).
[snip]
I think the above explains how attrgetter works with multiple
arguments. Here's a demo:
>>> class X:
... a = 2
... b = 3
... c = 1
...
>>> getter = operator.attrgetter('a', 'b', 'c')
>>> getter(X)
(2, 3, 1)
Oscar
More information about the Python-Dev
mailing list