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

Skip to content

Commit 0fbdf26

Browse files
committed
#16523: merge with 3.3.
2 parents 6f75a3e + babc822 commit 0fbdf26

3 files changed

Lines changed: 43 additions & 24 deletions

File tree

Doc/library/operator.rst

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

251251

252-
.. function:: attrgetter(attr[, args...])
252+
.. function:: attrgetter(attr)
253+
attrgetter(*attrs)
253254
254-
Return a callable object that fetches *attr* from its operand. If more than one
255-
attribute is requested, returns a tuple of attributes. After,
256-
``f = attrgetter('name')``, the call ``f(b)`` returns ``b.name``. After,
257-
``f = attrgetter('name', 'date')``, the call ``f(b)`` returns ``(b.name,
258-
b.date)``. Equivalent to::
255+
Return a callable object that fetches *attr* from its operand.
256+
If more than one attribute is requested, returns a tuple of attributes.
257+
The attribute names can also contain dots. For example:
258+
259+
* After ``f = attrgetter('name')``, the call ``f(b)`` returns ``b.name``.
260+
261+
* After ``f = attrgetter('name', 'date')``, the call ``f(b)`` returns
262+
``(b.name, b.date)``.
263+
264+
* After ``f = attrgetter('name.first', 'name.last')``, the call ``f(b)``
265+
returns ``(r.name.first, r.name.last)``.
266+
267+
Equivalent to::
259268

260269
def attrgetter(*items):
261270
if any(not isinstance(item, str) for item in items):
@@ -275,14 +284,19 @@ expect a function argument.
275284
return obj
276285

277286

278-
The attribute names can also contain dots; after ``f = attrgetter('date.month')``,
279-
the call ``f(b)`` returns ``b.date.month``.
280-
281-
.. function:: itemgetter(item[, args...])
287+
.. function:: itemgetter(item)
288+
itemgetter(*items)
282289
283290
Return a callable object that fetches *item* from its operand using the
284291
operand's :meth:`__getitem__` method. If multiple items are specified,
285-
returns a tuple of lookup values. Equivalent to::
292+
returns a tuple of lookup values. For example:
293+
294+
* After ``f = itemgetter(2)``, the call ``f(r)`` returns ``r[2]``.
295+
296+
* After ``g = itemgetter(2, 5, 3)``, the call ``g(r)`` returns
297+
``(r[2], r[5], r[3])``.
298+
299+
Equivalent to::
286300

287301
def itemgetter(*items):
288302
if len(items) == 1:
@@ -321,9 +335,14 @@ expect a function argument.
321335

322336
Return a callable object that calls the method *name* on its operand. If
323337
additional arguments and/or keyword arguments are given, they will be given
324-
to the method as well. After ``f = methodcaller('name')``, the call ``f(b)``
325-
returns ``b.name()``. After ``f = methodcaller('name', 'foo', bar=1)``, the
326-
call ``f(b)`` returns ``b.name('foo', bar=1)``. Equivalent to::
338+
to the method as well. For example:
339+
340+
* After ``f = methodcaller('name')``, the call ``f(b)`` returns ``b.name()``.
341+
342+
* After ``f = methodcaller('name', 'foo', bar=1)``, the call ``f(b)``
343+
returns ``b.name('foo', bar=1)``.
344+
345+
Equivalent to::
327346

328347
def methodcaller(name, *args, **kwargs):
329348
def caller(obj):

Lib/operator.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,9 @@ def length_hint(obj, default=0):
223223
class attrgetter:
224224
"""
225225
Return a callable object that fetches the given attribute(s) from its operand.
226-
After f=attrgetter('name'), the call f(r) returns r.name.
227-
After g=attrgetter('name', 'date'), the call g(r) returns (r.name, r.date).
228-
After h=attrgetter('name.first', 'name.last'), the call h(r) returns
226+
After f = attrgetter('name'), the call f(r) returns r.name.
227+
After g = attrgetter('name', 'date'), the call g(r) returns (r.name, r.date).
228+
After h = attrgetter('name.first', 'name.last'), the call h(r) returns
229229
(r.name.first, r.name.last).
230230
"""
231231
def __init__(self, attr, *attrs):
@@ -250,8 +250,8 @@ def __call__(self, obj):
250250
class itemgetter:
251251
"""
252252
Return a callable object that fetches the given item(s) from its operand.
253-
After f=itemgetter(2), the call f(r) returns r[2].
254-
After g=itemgetter(2,5,3), the call g(r) returns (r[2], r[5], r[3])
253+
After f = itemgetter(2), the call f(r) returns r[2].
254+
After g = itemgetter(2, 5, 3), the call g(r) returns (r[2], r[5], r[3])
255255
"""
256256
def __init__(self, item, *items):
257257
if not items:

Modules/_operator.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,8 @@ PyDoc_STRVAR(itemgetter_doc,
485485
"itemgetter(item, ...) --> itemgetter object\n\
486486
\n\
487487
Return a callable object that fetches the given item(s) from its operand.\n\
488-
After f=itemgetter(2), the call f(r) returns r[2].\n\
489-
After g=itemgetter(2,5,3), the call g(r) returns (r[2], r[5], r[3])");
488+
After f = itemgetter(2), the call f(r) returns r[2].\n\
489+
After g = itemgetter(2, 5, 3), the call g(r) returns (r[2], r[5], r[3])");
490490

491491
static PyTypeObject itemgetter_type = {
492492
PyVarObject_HEAD_INIT(NULL, 0)
@@ -737,9 +737,9 @@ PyDoc_STRVAR(attrgetter_doc,
737737
"attrgetter(attr, ...) --> attrgetter object\n\
738738
\n\
739739
Return a callable object that fetches the given attribute(s) from its operand.\n\
740-
After f=attrgetter('name'), the call f(r) returns r.name.\n\
741-
After g=attrgetter('name', 'date'), the call g(r) returns (r.name, r.date).\n\
742-
After h=attrgetter('name.first', 'name.last'), the call h(r) returns\n\
740+
After f = attrgetter('name'), the call f(r) returns r.name.\n\
741+
After g = attrgetter('name', 'date'), the call g(r) returns (r.name, r.date).\n\
742+
After h = attrgetter('name.first', 'name.last'), the call h(r) returns\n\
743743
(r.name.first, r.name.last).");
744744

745745
static PyTypeObject attrgetter_type = {

0 commit comments

Comments
 (0)