Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
babc8227
Commit
babc8227
authored
May 08, 2013
by
Ezio Melotti
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#16523: improve attrgetter/itemgetter/methodcaller documentation.
parent
c58a3ea7
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
40 additions
and
21 deletions
+40
-21
Doc/library/operator.rst
Doc/library/operator.rst
+33
-14
Modules/operator.c
Modules/operator.c
+7
-7
No files found.
Doc/library/operator.rst
View file @
babc8227
...
@@ -241,13 +241,22 @@ lookups. These are useful for making fast field extractors as arguments for
...
@@ -241,13 +241,22 @@ lookups. These are useful for making fast field extractors as arguments for
expect a function argument.
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
Return a callable object that fetches *attr* from its operand.
attribute is requested, returns a tuple of attributes. After,
If more than one attribute is requested, returns a tuple of attributes.
``f = attrgetter('name')``, the call ``f(b)`` returns ``b.name``. After,
The attribute names can also contain dots. For example:
``f = attrgetter('name', 'date')``, the call ``f(b)`` returns ``(b.name,
b.date)``. Equivalent to::
* 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):
def attrgetter(*items):
if any(not isinstance(item, str) for item in items):
if any(not isinstance(item, str) for item in items):
...
@@ -267,14 +276,19 @@ expect a function argument.
...
@@ -267,14 +276,19 @@ expect a function argument.
return obj
return obj
The attribute names can also contain dots; after ``f = attrgetter('date.month')``,
.. function:: itemgetter(item)
the call ``f(b)`` returns ``b.date.month``.
itemgetter(*items)
.. function:: itemgetter(item[, args...])
Return a callable object that fetches *item* from its operand using the
Return a callable object that fetches *item* from its operand using the
operand's :meth:`__getitem__` method. If multiple items are specified,
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):
def itemgetter(*items):
if len(items) == 1:
if len(items) == 1:
...
@@ -313,9 +327,14 @@ expect a function argument.
...
@@ -313,9 +327,14 @@ expect a function argument.
Return a callable object that calls the method *name* on its operand. If
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
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)``
to the method as well. For example:
returns ``b.name()``. After ``f = methodcaller('name', 'foo', bar=1)``, the
call ``f(b)`` returns ``b.name('foo', bar=1)``. Equivalent to::
* 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 methodcaller(name, *args, **kwargs):
def caller(obj):
def caller(obj):
...
...
Modules/operator.c
View file @
babc8227
...
@@ -460,8 +460,8 @@ PyDoc_STRVAR(itemgetter_doc,
...
@@ -460,8 +460,8 @@ PyDoc_STRVAR(itemgetter_doc,
"itemgetter(item, ...) --> itemgetter object
\n
\
"itemgetter(item, ...) --> itemgetter object
\n
\
\n
\
\n
\
Return a callable object that fetches the given item(s) from its operand.
\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
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
g = itemgetter(2, 5,
3), the call g(r) returns (r[2], r[5], r[3])"
);
static
PyTypeObject
itemgetter_type
=
{
static
PyTypeObject
itemgetter_type
=
{
PyVarObject_HEAD_INIT
(
NULL
,
0
)
PyVarObject_HEAD_INIT
(
NULL
,
0
)
...
@@ -712,9 +712,9 @@ PyDoc_STRVAR(attrgetter_doc,
...
@@ -712,9 +712,9 @@ PyDoc_STRVAR(attrgetter_doc,
"attrgetter(attr, ...) --> attrgetter object
\n
\
"attrgetter(attr, ...) --> attrgetter object
\n
\
\n
\
\n
\
Return a callable object that fetches the given attribute(s) from its operand.
\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
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
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
h =
attrgetter('name.first', 'name.last'), the call h(r) returns
\n
\
(r.name.first, r.name.last)."
);
(r.name.first, r.name.last)."
);
static
PyTypeObject
attrgetter_type
=
{
static
PyTypeObject
attrgetter_type
=
{
...
@@ -844,8 +844,8 @@ PyDoc_STRVAR(methodcaller_doc,
...
@@ -844,8 +844,8 @@ PyDoc_STRVAR(methodcaller_doc,
"methodcaller(name, ...) --> methodcaller object
\n
\
"methodcaller(name, ...) --> methodcaller object
\n
\
\n
\
\n
\
Return a callable object that calls the given method on its operand.
\n
\
Return a callable object that calls the given method on its operand.
\n
\
After
,
f = methodcaller('name'), the call f(r) returns r.name().
\n
\
After f = methodcaller('name'), the call f(r) returns r.name().
\n
\
After
,
g = methodcaller('name', 'date', foo=1), the call g(r) returns
\n
\
After g = methodcaller('name', 'date', foo=1), the call g(r) returns
\n
\
r.name('date', foo=1)."
);
r.name('date', foo=1)."
);
static
PyTypeObject
methodcaller_type
=
{
static
PyTypeObject
methodcaller_type
=
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment