Re: Commutative object in emulating numbers
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Commutative object in emulating numbers
On Sep 14, 8:16 am, Chris Rebert wrote: > On Sun, Sep 13, 2009 at 9:52 PM, iu2 wrote: > > Hi, > > > I reached the chapter "Emulating numeric types" in the python > > documentation and I tried this: > > class A: > > def __mul__(self, a): > > return 'A' * a > > > Now, this works as expected: > a = A() > a * 3 > > 'AAA' > > > But this doesn't (also as expected): > 3 * a > > > Traceback (most recent call last): > > File "", line 1, in > > 3 * a > > TypeError: unsupported operand type(s) for *: 'int' and 'instance' > > > What do I need to do in order to make the two classes, int and A, > > commutative? > > You need to define > __rmul__():http://docs.python.org/dev/3.0/reference/datamodel.html#object.__rmul__ > > Cheers, > Chris > --http://blog.rebertia.com- Hide quoted text - > > - Show quoted text - Thanks! -- http://mail.python.org/mailman/listinfo/python-list
Re: Commutative object in emulating numbers
On Sun, Sep 13, 2009 at 9:52 PM, iu2 wrote: > Hi, > > I reached the chapter "Emulating numeric types" in the python > documentation and I tried this: > class A: > def __mul__(self, a): > return 'A' * a > > Now, this works as expected: a = A() a * 3 > 'AAA' > > But this doesn't (also as expected): 3 * a > > Traceback (most recent call last): > File "", line 1, in > 3 * a > TypeError: unsupported operand type(s) for *: 'int' and 'instance' > > What do I need to do in order to make the two classes, int and A, > commutative? You need to define __rmul__(): http://docs.python.org/dev/3.0/reference/datamodel.html#object.__rmul__ Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Commutative object in emulating numbers
On Sep 14, 7:52 am, iu2 wrote: > Hi, > > I reached the chapter "Emulating numeric types" in the python > documentation and I tried this: > > >>> class A: > > def __mul__(self, a): > return 'A' * a > > Now, this works as expected:>>> a = A() > >>> a * 3 > > 'AAA' > > But this doesn't (also as expected): > > >>> 3 * a > > Traceback (most recent call last): > File "", line 1, in > 3 * a > TypeError: unsupported operand type(s) for *: 'int' and 'instance' > > > > What do I need to do in order to make the two classes, int and A, > commutative? > (In the same way that string and int are commutative over "*") > Thanks By commutative I mean give the same result, that is 3 * a will also return 'AAA' -- http://mail.python.org/mailman/listinfo/python-list
Commutative object in emulating numbers
Hi, I reached the chapter "Emulating numeric types" in the python documentation and I tried this: >>> class A: def __mul__(self, a): return 'A' * a Now, this works as expected: >>> a = A() >>> a * 3 'AAA' But this doesn't (also as expected): >>> 3 * a Traceback (most recent call last): File "", line 1, in 3 * a TypeError: unsupported operand type(s) for *: 'int' and 'instance' >>> What do I need to do in order to make the two classes, int and A, commutative? (In the same way that string and int are commutative over "*") Thanks -- http://mail.python.org/mailman/listinfo/python-list