[pypy-issue] Issue #1861: Presence of __index__ method in class hides some __r operators (pypy/pypy)

mcesar issues-reply at bitbucket.org
Fri Sep 5 18:25:06 CEST 2014


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).




More information about the pypy-issue mailing list