Skip to content

Instantly share code, notes, and snippets.

@ewerybody
Created January 9, 2022 21:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ewerybody/5fc1e36e50bb48ff0a799140eea3a882 to your computer and use it in GitHub Desktop.
Save ewerybody/5fc1e36e50bb48ff0a799140eea3a882 to your computer and use it in GitHub Desktop.
All the operator __function_names__ with their according operator.
"""
TestCase for directly showing off which __operation__ function name
connects to what runtime operator like `__add__` is called on `+`
more : https://docs.python.org/3/reference/datamodel.html#emulating-numeric-types
"""
class TestClass:
def __add__(self, arg):
print(f'{self} + {arg}')
def __sub__(self, arg):
print(f'{self} - {arg}')
def __mul__(self, arg):
print(f'{self} * {arg}')
def __matmul__(self, arg):
print(f'{self} @ {arg}')
def __truediv__(self, arg):
print(f'{self} / {arg}')
def __floordiv__(self, arg):
print(f'{self} // {arg}')
def __mod__(self, arg):
print(f'{self} % {arg}')
def __pow__(self, arg):
print(f'{self} ** {arg}')
def __lt__(self, arg):
print(f'{self} < {arg}')
def __le__(self, arg):
# Note: No need for `return self` here!
print(f'{self} <= {arg}')
def __gt__(self, arg):
print(f'{self} > {arg}')
def __ge__(self, arg):
# Note: No need for `return self` here!
print(f'{self} >= {arg}')
def __and__(self, arg):
print(f'{self} & {arg}')
def __or__(self, arg):
print(f'{self} | {arg}')
def __xor__(self, arg):
print(f'{self} ^ {arg}')
def __lshift__(self, arg):
print(f'{self} << {arg}')
def __rshift__(self, arg):
print(f'{self} >> {arg}')
# Note: The instance_ops need `return self` (Or equivalent object)
# to not invalidate themselves!
def __iadd__(self, arg):
print(f'{self} += {arg}')
return self
def __isub__(self, arg):
print(f'{self} -= {arg}')
return self
def __imul__(self, arg):
print(f'{self} *= {arg}')
return self
def __itruediv__(self, arg):
print(f'{self} /= {arg}')
return self
def __ior__(self, arg):
print(f'{self} |= {arg}')
return self
def __pos__(self):
print(f'+{self}')
def __neg__(self):
print(f'-{self}')
def __invert__(self):
print(f'~{self}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment