Python operator Module
Example
Use a function form of the + operator:
import operator
print(operator.add(2, 3))
Try it Yourself »
Definition and Usage
The operator module provides function equivalents of Python's intrinsic operators.
Use it to pass operators as callables (e.g. to higher-order functions) and for item/attr getters.
Members
| Member | Description |
|---|---|
| abs() | Return the absolute value of obj. |
| add() | Return a + b. |
| and_() | Return the bitwise and of a and b. |
| attrgetter() | Return a callable that fetches object attributes. |
| eq() | Return a == b. |
| floordiv() | Return a // b. |
| ge() | Return a >= b. |
| gt() | Return a > b. |
| itemgetter() | Return a callable that fetches items by index/key. |
| le() | Return a <= b. |
| lt() | Return a < b. |
| methodcaller() | Return a callable that calls a method on its operand. |
| mod() | Return a % b. |
| mul() | Return a * b. |
| ne() | Return a != b. |
| neg() | Return -obj. |
| not_() | Return the logical negation of obj. |
| or_() | Return the bitwise or of a and b. |
| pos() | Return +obj. |
| pow() | Return a ** b. |
| sub() | Return a - b. |
| truediv() | Return a / b. |