Skip to main content

Lib/operator.py

Source:

cpython 3.14 @ ab2d84fe1023/Lib/operator.py

Lib/operator.py is the pure-Python fallback for the operator module (the C accelerator is Modules/_operator.c). It exports every Python operator as a function (operator.add, operator.mul, operator.getitem, etc.), plus the attrgetter, itemgetter, and methodcaller callable factories used for key functions in sorted and min/max.

Map

LinesSymbolRole
1-50imports, __all__Re-export from _operator; fallback definitions
51-150Arithmetic operatorsadd, sub, mul, truediv, floordiv, mod, pow, neg, pos, abs
151-220Comparison and bitwiselt, le, eq, ne, ge, gt, and_, or_, xor, lshift, rshift
221-280Sequence operatorsgetitem, setitem, delitem, contains, concat, repeat
281-350attrgetter, itemgetter, methodcallerCallable factories

Reading

Function wrappers

Each operator function is a thin wrapper that calls the corresponding dunder method. The operator.add(a, b) function is equivalent to a + b; using it as a key or reducing function avoids a lambda.

# CPython: Lib/operator.py:51 add (pure-Python fallback)
def add(a, b):
"Same as a + b."
return a + b

The C extension _operator provides optimized C versions of these. The Python file uses try: from _operator import *; except ImportError: pass to prefer C where available.

attrgetter

attrgetter('x.y.z') returns a callable that, when called with an object obj, evaluates obj.x.y.z. Dotted names are split on . at construction time and the getter calls getattr in a loop.

# CPython: Lib/operator.py:281 attrgetter
class attrgetter:
def __init__(self, *attrs):
self._attrs = attrs
self._getters = [self._make_getter(a) for a in attrs]

def _make_getter(self, attr):
if '.' not in attr:
return lambda obj: getattr(obj, attr)
names = attr.split('.')
def getter(obj):
for name in names:
obj = getattr(obj, name)
return obj
return getter

def __call__(self, obj):
if len(self._getters) == 1:
return self._getters[0](obj)
return tuple(g(obj) for g in self._getters)

itemgetter and methodcaller

itemgetter(key) returns lambda obj: obj[key]. When multiple keys are given, it returns a tuple. methodcaller('name', *args, **kwargs) returns lambda obj: obj.name(*args, **kwargs). Both are used as key= arguments to sorted, min, max.

gopy notes

Not yet ported. The planned package path is module/operator/. The arithmetic wrappers are one-liners once the Go object protocol is in place. attrgetter and itemgetter require the __getattr__ and __getitem__ protocols which are implemented in objects/protocol.go.