Message5861

Author doublep
Recipients akong, babelmania, doublep
Date 2010-06-29.10:17:10
SpamBayes Score 4.1189733e-06
Marked as misclassified No
Message-id <1277806632.0.0.670401499355.issue1622@psf.upfronthosting.co.za>
In-reply-to
Content
I agree that it's a bug.  The whole point of __r*__ methods is to add support for operations in reversed situation where first class doesn't know about the second.  If works perfectly fine in CPython:

class Foo (object):
    def __add__(self, that):
        print self, that
    def __radd__(self, that):
        print self, that

[] + Foo ()
Foo () + []

This never throws TypeError.

The correct behavior for PyArray is to _return_ NotImplemented, not raise a TypeError.  E.g. consider the following example:

class Foo (object):
    def __add__(self, that):
        return NotImplemented
    def __radd__(self, that):
        return NotImplemented

class Bar (object):
    def __add__(self, that):
        print self, that
    def __radd__(self, that):
        print self, that

Foo () + Bar ()
Bar () + Foo ()
Foo () + 1

Class Foo is declared to never allow any addition whatsoever.  However, Bar can override this with __radd__ method and it works fine.  But the last statement fails, because neither Foo.__add__(int) nor int.__radd__(Foo) produces a result:

Traceback (most recent call last):
  File "/home/developer/test/test.py", line 15, in <module>
    Foo () + 1
TypeError: unsupported operand type(s) for +: 'Foo' and 'int'
History
Date User Action Args
2010-06-29 10:17:12doublepsetmessageid: <1277806632.0.0.670401499355.issue1622@psf.upfronthosting.co.za>
2010-06-29 10:17:11doublepsetrecipients: + doublep, akong, babelmania
2010-06-29 10:17:11doubleplinkissue1622 messages
2010-06-29 10:17:10doublepcreate