On this page
operator
Prev: opcode | Up: stdlib_module | Next: optparse
Rel: itertools, functools
Ref: docs.python.org — operator
Tags: #public #python3 #stdlib
Import
import operator
Overview
C-implemented functional equivalents of Python's built-in operators (add, mul, lt, eq, and_) plus fast, picklable field extractors (itemgetter, attrgetter, methodcaller). Reach for itemgetter and attrgetter in sorted(), min(), max(), and itertools.groupby() instead of repetitive lambda functions.
Quick Start
from operator import attrgetter, itemgetter
rows = [("Ada", 95), ("Lin", 88), ("Grace", 99)]
assert sorted(rows, key=itemgetter(1), reverse=True)[0] == ("Grace", 99)
by_name = itemgetter("name")
assert by_name({"name": "Ada", "role": "admin"}) == "Ada"
Implementation Walkthrough
1. Multi-Key Sorting with attrgetter & itemgetter
Both support multi-key extraction (returning a tuple) and attrgetter even traverses dotted attribute paths:
from dataclasses import dataclass
from operator import attrgetter
@dataclass(frozen=True)
class Task:
priority: int
title: str
tasks = [Task(2, "b"), Task(1, "z"), Task(1, "a")]
ordered = sorted(tasks, key=attrgetter("priority", "title"))
assert [(t.priority, t.title) for t in ordered] == [(1, "a"), (1, "z"), (2, "b")]
2. Calling a Named Method Across a Collection (methodcaller)
from operator import methodcaller
words = [" alpha ", " beta "]
cleaned = list(map(methodcaller("strip"), words))
assert cleaned == ["alpha", "beta"]
Gotchas & Boundaries
- Single-key vs. multi-key return type:
itemgetter(0)(seq)returns a single element, whereasitemgetter(0, 1)(seq)returns a 2-tuple. - Trailing underscores avoid Python keywords: Use
operator.and_,operator.or_,operator.not_,operator.is_, andoperator.is_not. - In-place variants (
iadd,ior): Mutate mutable targets in place and still return the result (essential for immutable targets likeintortuple).
Common Functions
- operator.abs() —
abs(a, /)— Same as abs(a). - operator.add() —
add(a, b, /)— Same as a + b. - operator.and_() —
and_(a, b, /)— Same as a & b. - operator.call() —
call(obj, /, *args, **kwargs)— Same as obj(*args, **kwargs). - operator.concat() —
concat(a, b, /)— Same as a + b, for a and b sequences. - operator.contains() —
contains(a, b, /)— Same as b in a (note reversed operands). - operator.countOf() —
countOf(a, b, /)— Return the number of items in a which are, or which equal, b. - operator.delitem() —
delitem(a, b, /)— Same as del a[b]. - operator.eq() —
eq(a, b, /)— Same as a == b. - operator.floordiv() —
floordiv(a, b, /)— Same as a // b. - operator.ge() —
ge(a, b, /)— Same as a >= b. - operator.getitem() —
getitem(a, b, /)— Same as a[b]. - operator.gt() —
gt(a, b, /)— Same as a > b. - operator.iadd() —
iadd(a, b, /)— Same as a += b. - operator.iand() —
iand(a, b, /)— Same as a &= b. - operator.iconcat() —
iconcat(a, b, /)— Same as a += b, for a and b sequences. - operator.ifloordiv() —
ifloordiv(a, b, /)— Same as a //= b. - operator.ilshift() —
ilshift(a, b, /)— Same as a <<= b. - operator.imatmul() —
imatmul(a, b, /)— Same as a @= b. - operator.imod() —
imod(a, b, /)— Same as a %= b. - operator.imul() —
imul(a, b, /)— Same as a *= b. - operator.index() —
index(a, /)— Same as a.index() - operator.indexOf() —
indexOf(a, b, /)— Return the first index of b in a. - operator.inv() —
inv(a, /)— Same as ~a. - operator.invert() —
invert(a, /)— Same as ~a. - operator.ior() —
ior(a, b, /)— Same as a |= b. - operator.ipow() —
ipow(a, b, /)— Same as a **= b. - operator.irshift() —
irshift(a, b, /)— Same as a >>= b. - operator.is_() —
is_(a, b, /)— Same as a is b. - operator.is_none() —
is_none(a, /)— Same as a is None. - operator.is_not() —
is_not(a, b, /)— Same as a is not b. - operator.is_not_none() —
is_not_none(a, /)— Same as a is not None. - operator.isub() —
isub(a, b, /)— Same as a -= b. - operator.itruediv() —
itruediv(a, b, /)— Same as a /= b. - operator.ixor() —
ixor(a, b, /)— Same as a ^= b.
Classes & Objects
- operator.attrgetter — Return a callable object that fetches the given attribute(s) from its operand.
- operator.itemgetter — Return a callable object that fetches the given item(s) from its operand.
- operator.methodcaller — Return a callable object that calls the given method on its operand.
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/functools
- tuts/python3/stdlib_module/itertools
- tuts/python3/stdlib_module/opcode
- tuts/python3/stdlib_module/operator/abs
- tuts/python3/stdlib_module/operator/add
- tuts/python3/stdlib_module/operator/and_
- tuts/python3/stdlib_module/operator/attrgetter
- tuts/python3/stdlib_module/operator/call
- tuts/python3/stdlib_module/operator/concat
- tuts/python3/stdlib_module/operator/contains
- tuts/python3/stdlib_module/operator/countOf
- tuts/python3/stdlib_module/operator/delitem
- tuts/python3/stdlib_module/operator/eq
- tuts/python3/stdlib_module/operator/floordiv
- tuts/python3/stdlib_module/operator/ge
- tuts/python3/stdlib_module/operator/getitem
- tuts/python3/stdlib_module/operator/gt
- tuts/python3/stdlib_module/operator/iadd
- tuts/python3/stdlib_module/operator/iand
- tuts/python3/stdlib_module/operator/iconcat
- tuts/python3/stdlib_module/operator/ifloordiv
- tuts/python3/stdlib_module/operator/ilshift
- tuts/python3/stdlib_module/operator/imatmul
- tuts/python3/stdlib_module/operator/imod
- tuts/python3/stdlib_module/operator/imul
- tuts/python3/stdlib_module/operator/index
- tuts/python3/stdlib_module/operator/indexOf
- tuts/python3/stdlib_module/operator/inv
- tuts/python3/stdlib_module/operator/invert
- tuts/python3/stdlib_module/operator/ior
- tuts/python3/stdlib_module/operator/ipow
- tuts/python3/stdlib_module/operator/irshift
- tuts/python3/stdlib_module/operator/is_
- tuts/python3/stdlib_module/operator/is_none
- tuts/python3/stdlib_module/operator/is_not
- tuts/python3/stdlib_module/operator/is_not_none
- tuts/python3/stdlib_module/operator/isub
- tuts/python3/stdlib_module/operator/itemgetter
- tuts/python3/stdlib_module/operator/itruediv
- tuts/python3/stdlib_module/operator/ixor
- tuts/python3/stdlib_module/operator/methodcaller
- tuts/python3/stdlib_module/optparse
- tuts/python3/stdlib_module/stdlib_module
Related notes
- tuts/python3/stdlib_module/functools Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/itertools Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/opcode Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/operator/abs Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/operator/add Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/operator/and_ Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/operator/attrgetter Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/operator/call Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/operator/concat Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/operator/contains Linked here; links to this page; shared tags: #stdlib