Assigning an operation to a special character within a class in Python

Viewed 50

So, numpy can call its method "matmul" when multiplying 2 numpy arrays [1] by using the "@" character:

import numpy as np

a = np.array([1,2,3])
b = np.array([4,5,6])

a@b

My question is, can I assign to an unusual character, let's say "$", some method within a class? For example, let's say I have a class "Fraction", so that when I call Fraction(1,2) $ Fraction(1,2) it returns the sum of the inputs plus 30?

So

Fraction(1,2) + Fraction(1,2)#will return Fraction(1) (which is equal to 1)

Then

Fraction(1,2) $ Fraction(1,2) #I want it to be equal to (Fraction(1,2) + Fraction(1,2) + 30), i.e 1 + 30 = 31

If It's not possible, how did numpy manage to do it with "@"?

0 Answers
Related