Kaydet (Commit) 0fbdf261 authored tarafından Ezio Melotti's avatar Ezio Melotti

#16523: merge with 3.3.

......@@ -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):
......
......@@ -223,9 +223,9 @@ def length_hint(obj, default=0):
class attrgetter:
"""
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
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).
"""
def __init__(self, attr, *attrs):
......@@ -250,8 +250,8 @@ class attrgetter:
class itemgetter:
"""
Return a callable object that fetches the given item(s) from its operand.
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])
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])
"""
def __init__(self, item, *items):
if not items:
......
......@@ -485,8 +485,8 @@ PyDoc_STRVAR(itemgetter_doc,
"itemgetter(item, ...) --> itemgetter object\n\
\n\
Return a callable object that fetches the given item(s) from its operand.\n\
After f=itemgetter(2), the call f(r) returns r[2].\n\
After g=itemgetter(2,5,3), the call g(r) returns (r[2], r[5], r[3])");
After f = itemgetter(2), the call f(r) returns r[2].\n\
After g = itemgetter(2, 5, 3), the call g(r) returns (r[2], r[5], r[3])");
static PyTypeObject itemgetter_type = {
PyVarObject_HEAD_INIT(NULL, 0)
......@@ -737,9 +737,9 @@ PyDoc_STRVAR(attrgetter_doc,
"attrgetter(attr, ...) --> attrgetter object\n\
\n\
Return a callable object that fetches the given attribute(s) from its operand.\n\
After f=attrgetter('name'), the call f(r) returns r.name.\n\
After g=attrgetter('name', 'date'), the call g(r) returns (r.name, r.date).\n\
After h=attrgetter('name.first', 'name.last'), the call h(r) returns\n\
After f = attrgetter('name'), the call f(r) returns r.name.\n\
After g = attrgetter('name', 'date'), the call g(r) returns (r.name, r.date).\n\
After h = attrgetter('name.first', 'name.last'), the call h(r) returns\n\
(r.name.first, r.name.last).");
static PyTypeObject attrgetter_type = {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment