Python operator - itemgetter, attrgetter, methodcaller and function forms of operators
Answer: itemgetter(), attrgetter() and methodcaller() each return a callable object that fetches an item, fetches an attribute, or calls a method on its operand, which suits key= arguments. The operator module exports efficient functions corresponding to the intrinsic operators of Python.
Examples
from operator import itemgetter, attrgetter, methodcaller, add, mul
from functools import reduce
rows = [("apple", 3), ("pear", 1), ("fig", 2)]
sorted(rows, key=itemgetter(1)) # by second item
itemgetter(0, 1)(rows[0]) # ('apple', 3)
class User:
def __init__(self, name, age):
self.name, self.age = name, age
users = [User("ana", 31), User("bo", 25)]
sorted(users, key=attrgetter("age", "name"))
words = [" a ", " b"]
list(map(methodcaller("strip"), words)) # ['a', 'b']
reduce(mul, [1, 2, 3, 4]) # 24
list(map(add, [1, 2], [10, 20])) # [11, 22]
Getter helpers
| Function | Returns a callable object that | |---|---| | itemgetter(2) | fetches item 2 with __getitem__(); several items return a tuple | | attrgetter('name') | fetches the attribute; names can contain dots like 'name.first' | | methodcaller('name', 'foo', bar=1) | calls obj.name('foo', bar=1) |
Operator functions
operator.add(x, y)is equivalent to the expressionx+y.- Names follow the special methods without the double underscores, such as
truediv,floordiv,contains,and_,or_andxor. The variants without underscores are preferred for clarity. contains(seq, obj)isobj in seq;concat(seq1, seq2)isseq1 + seq2.
In-place functions
x += y is equivalent to x = operator.iadd(x, y). The in-place functions only call the in-place method; you still assign the result yourself. For immutable targets such as strings and numbers, the value is computed but not stored back.
See also
Claims
- id: C1 claim: operator.add(x, y) equals x+y. source: S1 evidence: "operator.add(x, y) is equivalent to the expression x+y"
- id: C2 claim: Variants without double underscores are preferred. source: S1 evidence: "The variants without the double underscores are preferred for clarity."
- id: C3 claim: The module exports functions for Python's intrinsic operators. source: S2 evidence: "This module exports a set of functions corresponding to the intrinsic"
Sources
- [S1] operator - Standard operators as functions. Python docs. https://docs.python.org/3/library/operator.html . License: PSF-2.0. Accessed 2026-11-18.
- [S2] Lib/operator.py. python/cpython (GitHub). https://github.com/python/cpython/blob/main/Lib/operator.py . License: PSF-2.0. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, operator - Standard operators as functions; python/cpython (GitHub), Lib/operator.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python docs, operator - Standard operators as functions; python/cpython (GitHub), Lib/operator.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.
Cite this
iinventedlight. 2026. "Python operator - itemgetter, attrgetter, methodcaller and function forms of operators." iinventedlight Library, iil:note:q015277k, version of 2026-11-18. https://iinventedlight.com/library/notes/python-operator/. License: CC-BY-4.0.
@misc{iil_note_q015277k,
author = {{iinventedlight}},
title = {{Python operator - itemgetter, attrgetter, methodcaller and function forms of operators}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/python-operator/},
note = {Permanent ID iil:note:q015277k. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- python-operator.table.json (table; CC-BY-4.0)
- python-operator.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-operator.md.