Python: multiplication override Python: multiplication override python python

Python: multiplication override


Just add the following to the class definition and you should be good to go:

__rmul__ = __mul__


Implement __rmul__ as well.

class Foo(object):    def __mul__(self, other):        print '__mul__'        return other    def __rmul__(self, other):        print '__rmul__'        return otherx = Foo()2 * x # __rmul__x * 2 # __mul__