I
iu2
Hi,
I reached the chapter "Emulating numeric types" in the python
documentation and I tried this:
def __mul__(self, a):
return 'A' * a
Now, this works as expected:'AAA'
But this doesn't (also as expected):
Traceback (most recent call last):
File "<pyshell#45>", line 1, in <module>
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
I reached the chapter "Emulating numeric types" in the python
documentation and I tried this:
def __mul__(self, a):
return 'A' * a
Now, this works as expected:'AAA'
But this doesn't (also as expected):
Traceback (most recent call last):
File "<pyshell#45>", line 1, in <module>
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