diff options
author | Ezio Melotti <ezio.melotti@gmail.com> | 2013-05-08 07:56:32 (GMT) |
---|---|---|
committer | Ezio Melotti <ezio.melotti@gmail.com> | 2013-05-08 07:56:32 (GMT) |
commit | 0fbdf261582b16e409ae929bdf9107ae9fe817fb (patch) | |
tree | 8277c639350d69afb59608effe7038385cb615d3 /Doc/library/operator.rst | |
parent | 6f75a3e89ededd669be6517fe6f941c7a930df1b (diff) | |
parent | babc8227cc842287cf701674f2807c392b7a6350 (diff) | |
download | cpython-0fbdf261582b16e409ae929bdf9107ae9fe817fb.zip cpython-0fbdf261582b16e409ae929bdf9107ae9fe817fb.tar.gz cpython-0fbdf261582b16e409ae929bdf9107ae9fe817fb.tar.bz2 |
#16523: merge with 3.3.
Diffstat (limited to 'Doc/library/operator.rst')
-rw-r--r-- | Doc/library/operator.rst | 47 |
1 files changed, 33 insertions, 14 deletions
diff --git a/Doc/library/operator.rst b/Doc/library/operator.rst index 877a790..650b3df 100644 --- a/Doc/library/operator.rst +++ b/Doc/library/operator.rst @@ -249,13 +249,22 @@ lookups. These are useful for making fast field extractors as arguments for expect a function argument. -.. function:: attrgetter(attr[, args...]) +.. function:: attrgetter(attr) + attrgetter(*attrs) - Return a callable object that fetches *attr* from its operand. If more than one - attribute is requested, returns a tuple of attributes. After, - ``f = attrgetter('name')``, the call ``f(b)`` returns ``b.name``. After, - ``f = attrgetter('name', 'date')``, the call ``f(b)`` returns ``(b.name, - b.date)``. Equivalent to:: + Return a callable object that fetches *attr* from its operand. + If more than one attribute is requested, returns a tuple of attributes. + The attribute names can also contain dots. For example: + + * After ``f = attrgetter('name')``, the call ``f(b)`` returns ``b.name``. + + * After ``f = attrgetter('name', 'date')``, the call ``f(b)`` returns + ``(b.name, b.date)``. + + * After ``f = attrgetter('name.first', 'name.last')``, the call ``f(b)`` + returns ``(r.name.first, r.name.last)``. + + Equivalent to:: def attrgetter(*items): if any(not isinstance(item, str) for item in items): @@ -275,14 +284,19 @@ expect a function argument. return obj - The attribute names can also contain dots; after ``f = attrgetter('date.month')``, - the call ``f(b)`` returns ``b.date.month``. - -.. function:: itemgetter(item[, args...]) +.. function:: itemgetter(item) + itemgetter(*items) Return a callable object that fetches *item* from its operand using the operand's :meth:`__getitem__` method. If multiple items are specified, - returns a tuple of lookup values. Equivalent to:: + returns a tuple of lookup values. For example: + + * After ``f = itemgetter(2)``, the call ``f(r)`` returns ``r[2]``. + + * After ``g = itemgetter(2, 5, 3)``, the call ``g(r)`` returns + ``(r[2], r[5], r[3])``. + + Equivalent to:: def itemgetter(*items): if len(items) == 1: @@ -321,9 +335,14 @@ expect a function argument. Return a callable object that calls the method *name* on its operand. If additional arguments and/or keyword arguments are given, they will be given - to the method as well. After ``f = methodcaller('name')``, the call ``f(b)`` - returns ``b.name()``. After ``f = methodcaller('name', 'foo', bar=1)``, the - call ``f(b)`` returns ``b.name('foo', bar=1)``. Equivalent to:: + to the method as well. For example: + + * After ``f = methodcaller('name')``, the call ``f(b)`` returns ``b.name()``. + + * After ``f = methodcaller('name', 'foo', bar=1)``, the call ``f(b)`` + returns ``b.name('foo', bar=1)``. + + Equivalent to:: def methodcaller(name, *args, **kwargs): def caller(obj): |