Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit babc822

Browse files
committed
#16523: improve attrgetter/itemgetter/methodcaller documentation.
1 parent c58a3ea commit babc822

2 files changed

Lines changed: 40 additions & 21 deletions

File tree

Doc/library/operator.rst

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -241,13 +241,22 @@ lookups. These are useful for making fast field extractors as arguments for
241241
expect a function argument.
242242

243243

244-
.. function:: attrgetter(attr[, args...])
244+
.. function:: attrgetter(attr)
245+
attrgetter(*attrs)
245246
246-
Return a callable object that fetches *attr* from its operand. If more than one
247-
attribute is requested, returns a tuple of attributes. After,
248-
``f = attrgetter('name')``, the call ``f(b)`` returns ``b.name``. After,
249-
``f = attrgetter('name', 'date')``, the call ``f(b)`` returns ``(b.name,
250-
b.date)``. Equivalent to::
247+
Return a callable object that fetches *attr* from its operand.
248+
If more than one attribute is requested, returns a tuple of attributes.
249+
The attribute names can also contain dots. For example:
250+
251+
* After ``f = attrgetter('name')``, the call ``f(b)`` returns ``b.name``.
252+
253+
* After ``f = attrgetter('name', 'date')``, the call ``f(b)`` returns
254+
``(b.name, b.date)``.
255+
256+
* After ``f = attrgetter('name.first', 'name.last')``, the call ``f(b)``
257+
returns ``(r.name.first, r.name.last)``.
258+
259+
Equivalent to::
251260

252261
def attrgetter(*items):
253262
if any(not isinstance(item, str) for item in items):
@@ -267,14 +276,19 @@ expect a function argument.
267276
return obj
268277

269278

270-
The attribute names can also contain dots; after ``f = attrgetter('date.month')``,
271-
the call ``f(b)`` returns ``b.date.month``.
272-
273-
.. function:: itemgetter(item[, args...])
279+
.. function:: itemgetter(item)
280+
itemgetter(*items)
274281
275282
Return a callable object that fetches *item* from its operand using the
276283
operand's :meth:`__getitem__` method. If multiple items are specified,
277-
returns a tuple of lookup values. Equivalent to::
284+
returns a tuple of lookup values. For example:
285+
286+
* After ``f = itemgetter(2)``, the call ``f(r)`` returns ``r[2]``.
287+
288+
* After ``g = itemgetter(2, 5, 3)``, the call ``g(r)`` returns
289+
``(r[2], r[5], r[3])``.
290+
291+
Equivalent to::
278292

279293
def itemgetter(*items):
280294
if len(items) == 1:
@@ -313,9 +327,14 @@ expect a function argument.
313327

314328
Return a callable object that calls the method *name* on its operand. If
315329
additional arguments and/or keyword arguments are given, they will be given
316-
to the method as well. After ``f = methodcaller('name')``, the call ``f(b)``
317-
returns ``b.name()``. After ``f = methodcaller('name', 'foo', bar=1)``, the
318-
call ``f(b)`` returns ``b.name('foo', bar=1)``. Equivalent to::
330+
to the method as well. For example:
331+
332+
* After ``f = methodcaller('name')``, the call ``f(b)`` returns ``b.name()``.
333+
334+
* After ``f = methodcaller('name', 'foo', bar=1)``, the call ``f(b)``
335+
returns ``b.name('foo', bar=1)``.
336+
337+
Equivalent to::
319338

320339
def methodcaller(name, *args, **kwargs):
321340
def caller(obj):

Modules/operator.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,8 @@ PyDoc_STRVAR(itemgetter_doc,
460460
"itemgetter(item, ...) --> itemgetter object\n\
461461
\n\
462462
Return a callable object that fetches the given item(s) from its operand.\n\
463-
After, f=itemgetter(2), the call f(r) returns r[2].\n\
464-
After, g=itemgetter(2,5,3), the call g(r) returns (r[2], r[5], r[3])");
463+
After f = itemgetter(2), the call f(r) returns r[2].\n\
464+
After g = itemgetter(2, 5, 3), the call g(r) returns (r[2], r[5], r[3])");
465465

466466
static PyTypeObject itemgetter_type = {
467467
PyVarObject_HEAD_INIT(NULL, 0)
@@ -712,9 +712,9 @@ PyDoc_STRVAR(attrgetter_doc,
712712
"attrgetter(attr, ...) --> attrgetter object\n\
713713
\n\
714714
Return a callable object that fetches the given attribute(s) from its operand.\n\
715-
After, f=attrgetter('name'), the call f(r) returns r.name.\n\
716-
After, g=attrgetter('name', 'date'), the call g(r) returns (r.name, r.date).\n\
717-
After, h=attrgetter('name.first', 'name.last'), the call h(r) returns\n\
715+
After f = attrgetter('name'), the call f(r) returns r.name.\n\
716+
After g = attrgetter('name', 'date'), the call g(r) returns (r.name, r.date).\n\
717+
After h = attrgetter('name.first', 'name.last'), the call h(r) returns\n\
718718
(r.name.first, r.name.last).");
719719

720720
static PyTypeObject attrgetter_type = {
@@ -844,8 +844,8 @@ PyDoc_STRVAR(methodcaller_doc,
844844
"methodcaller(name, ...) --> methodcaller object\n\
845845
\n\
846846
Return a callable object that calls the given method on its operand.\n\
847-
After, f = methodcaller('name'), the call f(r) returns r.name().\n\
848-
After, g = methodcaller('name', 'date', foo=1), the call g(r) returns\n\
847+
After f = methodcaller('name'), the call f(r) returns r.name().\n\
848+
After g = methodcaller('name', 'date', foo=1), the call g(r) returns\n\
849849
r.name('date', foo=1).");
850850

851851
static PyTypeObject methodcaller_type = {

0 commit comments

Comments
 (0)