__iadd__ and __isub__ map to += and -= but don't return result

Fredrik Lundh fredrik at pythonware.com
Fri Apr 8 13:08:35 EDT 2005


Anthra Norell wrote:

> If I am missing a point here, what could it be?

the documentation?

> class Vertex (list):
>    def __init__ (self, *coordinates): self [:] = list (coordinates [0:2])
>    def __add__ (self, V):  return Vertex (self [X] + V [X], self [Y] + V [Y])
>    def __iadd__ (self, V): self [X] += V [X]; self [Y] += V [Y]

> >>> V1 += V2      # ***
> V1                # ***
>                   # *** died ?
> >>> print V1
> None              # *** V1 died !

if you leave out the return statement, Python automagically inserts a "return None"
at the end of your method.

and the __iadd__ documentation says:

    These methods are called to implement the augmented arithmetic operations
    (+=, -=, *=, /=, %=, **=, <<=, >>=, &=, ^=, |=). These methods should
    attempt to do the operation in-place (modifying self) and return the result
    (which could be, but does not have to be, self).

in other words, it works exactly as documented.  you return None, you get None.

</F> 






More information about the Python-list mailing list