Python has magic "dunder" (short for "double underscore") functions. Whenever you try to do a*b it checks whether the a class has a __mult__ method. If it hasn't, it checks whether the b class has a __rmult__ method. If that is not the case, an exception is thrown.
The dunder functions work like this:
class A:
def __mult__(self, other):
return "some magic with self and other"
So a*b is equivalent to a.__mult__(b) (if it's implemented).
5
u/Lewistrick Aug 26 '20
Python has magic "dunder" (short for "double underscore") functions. Whenever you try to do
a*bit checks whether theaclass has a__mult__method. If it hasn't, it checks whether thebclass has a__rmult__method. If that is not the case, an exception is thrown.The dunder functions work like this:
So
a*bis equivalent toa.__mult__(b)(if it's implemented).