Python 3.11+operatorfunction
operator.call
calls and access
Call an object: call(f, *args, **kwargs) == f(*args, **kwargs) (3.11+).
Syntax
operator.call(f, *args, **kwargs)
It lets you invoke a set of functions in one line: list(map(operator.call, callbacks)) runs each one with no arguments. With arguments it pairs well with itertools.starmap. It was added in Python 3.11; on older versions lambda f, *a: f(*a) does the same. Order matters: the callable comes first.
Examples
import operator
print(operator.call(abs, -5)) # → 5
print(operator.call(abs, -5) == abs(-5)) # → True
print(operator.call(round, 3.14159, ndigits=2)) # → 3.14
print([operator.call(f) for f in (list, dict, tuple)]) # → [[], {}, ()]
print(operator.call(5)) # → TypeError
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy