Operators

The Python operator module [0] contains a large variety of operators and iteration_utilities only tries to fill in some missing ones:

Reverse arithmetic operators

Function Equivalent
radd() lambda x, y: y + x
rsub() lambda x, y: y - x
rmul() lambda x, y: y * x
rdiv() lambda x, y: y / x
rfdiv() lambda x, y: y // x
rpow() lambda x, y: y ** x
rmod() lambda x, y: y % x

Math operators

And of course the standard operators from the operator module:

Function Equivalent
operator.add() (iadd) lambda x, y: x + y
operator.sub() (isub) lambda x, y: x - y
operator.mul() (imul) lambda x, y: x * y
operator.truediv() (itruediv) lambda x, y: x / y
operator.floordiv() (ifloordiv) lambda x, y: x // y
operator.pow() (ipow) lambda x, y: x ** y
operator.mod() (imod) lambda x, y: x % y
operator.matmul() (imatmul) lambda x, y: x @ y
operator.abs() lambda x: abs(x)
operator.pos() lambda x: +x
operator.neg() lambda x: -x

And the bitwise operators:

Function Equivalent
operator.lshift() (ilshift) lambda x, y: x << y
operator.rshift() (irshift) lambda x, y: x >> y
operator.and_() (iand) lambda x, y: x & y
operator.or_() (ior) lambda x, y: x | y
operator.xor() (ixor) lambda x, y: x ^ y
operator.inv() lambda x: ~x

Note

The math module contains several more!

Comparison operators

  • is_even(), equivalent to lambda x: (x % 2) == 0.
  • is_odd(), equivalent to lambda x: (x % 2) != 0.
  • is_None(), equivalent to lambda x: x is None.
  • is_not_None(), equivalent to lambda x: x is not None.
  • is_iterable(), roughly equivalent to lambda x: isinstance(x, collections.Iterable).

And the comparison operators from the Python library:

Function Equivalent
operator.lt() lambda x, y: x < y
operator.le() lambda x, y: x <= y
operator.eq() lambda x, y: x == y
operator.ne() lambda x, y: x |= y
operator.ge() lambda x, y: x >= y
operator.gt() lambda x, y: x < y
operator.is_() lambda x, y: x is y
operator.is_not() lambda x, y: x is not y
operator.truth() lambda x: not not x
operator.not_() lambda x: not x

Misc

And some misc operators

Function Equivalent
operator.index() * lambda x: x__index__()
operator.concat() (iconcat) * lambda x, y: x + y
operator.contains() lambda x, y: y in x
operator.countOf() * lambda x, y: y.count(x)
operator.indexOf() * lambda x, y: y.index(x)
operator.getitem() lambda x, y: x[y]
operator.setitem() lambda x, y, z: x[y] = z
operator.delitem() lambda x, y: del x[y]
operator.itemgetter() * lambda x, lambda y: y[x]
operator.attrgetter() * lambda x, lambda y: y.x
operator.methodcaller() * lambda x, lambda y: y.x()
operator.length_hint() * lambda x, y: len(x) or x.__length_hint__() or y

Marked (*) functions only have a rough equivalent and may be more sophisticated!