Reputation: 1644
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)
Upvotes: 1
Views: 605
Reputation: 5006
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)
Upvotes: 2