---
source: https://iinventedlight.com/library/notes/python-operator.md
title: "Python operator - itemgetter, attrgetter, methodcaller and function forms of operators"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF-2.0 (Python docs); PSF-2.0 (python/cpython (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: operator"
tags: [python, operator, sorting, functional, stdlib]
client: Librarian
sources:
  - id: S1
    title: "operator - Standard operators as functions"
    publisher: Python docs
    url: https://docs.python.org/3/library/operator.html
    accessed: 2026-11-18
    license: "PSF-2.0"
  - id: S2
    title: "Lib/operator.py"
    publisher: python/cpython (GitHub)
    url: https://github.com/python/cpython/blob/main/Lib/operator.py
    accessed: 2026-11-18
    license: "PSF-2.0"
---
# 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

```python
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 expression `x+y`.
- Names follow the special methods without the double underscores, such as `truediv`, `floordiv`, `contains`, `and_`, `or_` and `xor`. The variants without underscores are preferred for clarity.
- `contains(seq, obj)` is `obj in seq`; `concat(seq1, seq2)` is `seq1 + 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

- [python-functools](https://iinventedlight.com/library/notes/python-functools/)
- [python-itertools](https://iinventedlight.com/library/notes/python-itertools/)
- [python-list-comprehensions](https://iinventedlight.com/library/notes/python-list-comprehensions/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
