In article <4e77eae1$0$29978$c3e8da3$54964...@news.astraweb.com>,
 Steven D'Aprano <steve+comp.lang.pyt...@pearwood.info> wrote:

> Westley Martínez wrote:
> 
> > def __radd__(self, other):
> >     return self.__add__(self, other)
> 
> Which, inside a class, can be simplified to:
> 
>     __radd__ = __add__

Ooh, I could see that leading to some weird diagnostics.  For example:

class Foo:
    def __add__(self, other):
        raise Exception

    __radd__ = __add__

f1 = Foo()
print 1 + f1

produces:

./add.py
Traceback (most recent call last):
  File "./add.py", line 11, in <module>
    print 1 + f1
  File "./add.py", line 5, in __add__
    raise Exception
Exception

which leaves the user wondering why __add__() was called when clearly 
__radd__() should have been.  The way Westley wrote it (modulo fixing 
the __add__() call signature) produces:

./add.py
Traceback (most recent call last):
  File "./add.py", line 11, in <module>
    print 1 + f1
  File "./add.py", line 8, in __radd__
    return self.__add__(other)
  File "./add.py", line 5, in __add__
    raise Exception
Exception

which at least is a stack trace that shows that __radd__() was called.
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to