Commutative object in emulating numbers

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Mon Sep 14 01:23:27 EDT 2009


On Sun, 13 Sep 2009 21:52:26 -0700, iu2 wrote:

> Hi,
> 
> I reached the chapter "Emulating numeric types" in the python
> documentation and I tried this:
[...]
> What do I need to do in order to make the two classes, int and A,
> commutative?


Try adding a __rmul__ method:


class A:
    def __mul__(self, a):
        return 'A' * a
    __rmul__ = __mul__



-- 
Steven



More information about the Python-list mailing list