inconsistency with += between different types ?

Christian Tanzer tanzer at swing.co.at
Fri Aug 9 02:38:59 EDT 2002


"Delaney, Timothy" <tdelaney at avaya.com> wrote:

> > class I:
> >
> >     def __add__(self, other):
> >         ...
> >         return self
> >
> >     __radd__ = __iadd__
>                    ^
> >     __iadd__ = __add__
>
> Of course, I included a typo ... that should of course have been
>
>     __radd__ = __add__
>
> Would have been caught the first time the module was imported ;)

Still doesn't work:

    >>> class Int (int) :
    ...   def __add__(self, other):
    ...     print "Int.__add__"
    ...     int.__add__(self,other)
    ...     return self
    ...   __radd__ = __add__
    ...   __iadd__ = __add__
    ...
    >>> a = Int(5)
    >>> b = Int(7)
    >>> c = a + b
    Int.__add__
    >>> c
    5
    >>> c+=7
    Int.__add__
    >>> c
    5

Compare that to:

    >>> x = 5
    >>> y = 7
    >>> z = x + y
    >>> z
    12
    >>> z+=y
    >>> z
    19
    >>>

Maybe Andreas' proposal wasn't so bad after all?

-- 
Christian Tanzer                                         tanzer at swing.co.at
Glasauergasse 32                                       Tel: +43 1 876 62 36
A-1130 Vienna, Austria                                 Fax: +43 1 877 66 92





More information about the Python-list mailing list