8.3. Builtin Library
The operator module exports a set of efficient functions corresponding to the intrinsic operators of Python.
For example,
operator.add(x, y)
is equivalent to the expressionx+y
.Many function names are those used for special methods, without the double underscores.
For backward compatibility, many of these have a variant with the double underscores kept.
The variants without the double underscores are preferred for clarity.
8.3.1. Mapping Operators to Functions
Operation |
Syntax |
Function |
---|---|---|
Addition |
|
|
Concatenation |
|
|
Containment Test |
|
|
Division |
|
|
Division |
|
|
Bitwise And |
|
|
Bitwise Exclusive Or |
|
|
Bitwise Inversion |
|
|
Bitwise Or |
|
|
Exponentiation |
|
|
Identity |
|
|
Identity |
|
|
Indexed Assignment |
|
|
Indexed Deletion |
|
|
Indexing |
|
|
Left Shift |
|
|
Modulo |
|
|
Multiplication |
|
|
Matrix Multiplication |
|
|
Negation (Arithmetic) |
|
|
Negation (Logical) |
|
|
Positive |
|
|
Right Shift |
|
|
Slice Assignment |
|
|
Slice Deletion |
|
|
Slicing |
|
|
String Formatting |
|
|
Subtraction |
|
|
Truth Test |
|
|
Ordering |
|
|
Ordering |
|
|
Equality |
|
|
Difference |
|
|
Ordering |
|
|
Ordering |
|
|
8.3.2. Comparison
operator.lt(a, b)
operator.le(a, b)
operator.eq(a, b)
operator.ne(a, b)
operator.ge(a, b)
operator.gt(a, b)
operator.not_(obj)
operator.truth(obj)
operator.is_(a, b)
operator.is_not(a, b)
operator.abs(obj)
operator.add(a, b)
operator.and_(a, b)
operator.floordiv(a, b)
operator.index(a)
operator.invert(obj)
oroperator.inv(obj)
operator.lshift(a, b)
operator.mod(a, b)
operator.mul(a, b)
operator.matmul(a, b)
operator.neg(obj)
operator.or_(a, b)
operator.pos(obj)
operator.pow(a, b)
operator.rshift(a, b)
operator.sub(a, b)
operator.truediv(a, b)
operator.xor(a, b)
operator.concat(a, b)
operator.contains(a, b)
operator.countOf(a, b)
operator.delitem(a, b)
operator.getitem(a, b)
operator.indexOf(a, b)
operator.setitem(a, b, c)
operator.length_hint(obj, default=0)
operator.attrgetter(attr) or operator.attrgetter(*attrs)
operator.itemgetter(item) or operator.itemgetter(*items)
operator.methodcaller(name, /, *args, **kwargs)
8.3.3. In-place Operators
operator.iadd(a, b)
operator.iand(a, b)
operator.iconcat(a, b)
operator.ifloordiv(a, b)
operator.ilshift(a, b)
operator.imod(a, b)
operator.imul(a, b)
operator.imatmul(a, b)
operator.ior(a, b)
operator.ipow(a, b)
operator.irshift(a, b)
operator.isub(a, b)
operator.itruediv(a, b)
operator.ixor(a, b)