Is it possible to define my own mathematical operator?
Like the introduction of the new @
in PEP-465 for matrix multiplication or //
for integer division.
Something like
a ! b -> a / (a + b)
Is it possible to define my own mathematical operator?
Like the introduction of the new @
in PEP-465 for matrix multiplication or //
for integer division.
Something like
a ! b -> a / (a + b)
You can use one of the predefined operator that has not been implemented for integers.
For example this one : @ (matmul)
You cannot use !
character as it is not in this list.
class MyInt(int):
def __matmul__(self, other):
return self / (self + other)
Then you can do :
a = MyInt(1)
b = MyInt(2)
print(a @ b)
Will print (0.33333333)