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
2d55e2aa
Commit
2d55e2aa
authored
Aug 21, 2010
by
Benjamin Peterson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
provide sample implementations for attrgetter and methodcaller
parent
c16f8b33
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
23 additions
and
2 deletions
+23
-2
Doc/library/operator.rst
Doc/library/operator.rst
+23
-2
No files found.
Doc/library/operator.rst
View file @
2d55e2aa
...
...
@@ -333,7 +333,23 @@ expect a function argument.
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)``.
b.date)``. Equivalent to::
def attrgetter(*items):
if len(items) == 1:
attr = items[0]
def g(obj):
return resolve_attr(obj, attr)
else:
def g(obj):
return tuple(resolve_att(obj, attr) for attr in items)
return g
def resolve_attr(obj, attr):
for name in attr.split("."):
obj = getattr(obj, name)
return obj
The attribute names can also contain dots; after ``f = attrgetter('date.month')``,
the call ``f(b)`` returns ``b.date.month``.
...
...
@@ -383,7 +399,12 @@ expect a function argument.
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)``.
call ``f(b)`` returns ``b.name('foo', bar=1)``. Equivalent to::
def methodcaller(name, *args, **kwargs):
def caller(obj):
return getattr(obj, name)(*args, **kwargs)
return caller
.. _operator-map:
...
...
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