New issue 1861: Presence of __index__ method in class hides some __r operators
https://bitbucket.org/pypy/pypy/issue/1861/presence-of-__index__-method-in-class
mcesar:
Hi all, this code:
```
#!python
class oops:
def __init__(self, val):
self.val = val
def __rmul__(self, other):
if isinstance(other, str):
other = int(other)
return self.val * other
a = oops(3)
print((2 * a, '2' * a))
```
when run returns: (6, 6)
But if the _ _ index _ _ method is added to the class:
```
#!python
class oops:
def __init__(self, val):
self.val = val
def __rmul__(self, other):
if isinstance(other, str):
other = int(other)
return self.val * other
def __index__(self):
return int(self.val)
a = oops(3)
print((2 * a, '2' * a))
```
the output is: (6, '222')
Seems that adding the _ _ index _ _ method to the class forces pypy to catch
the * operator as the _ _ mul _ _ operator of the string, instead of the _ _
rmul _ _ of the oops.
This happens in both 2.X and 3.X flavours of pypy.
CPython returns (6, 6) in both cases (and also in 3.4 and 2.7 versions).
_______________________________________________
pypy-issue mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-issue