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

Anthra Norell anthra.norell at tiscalinet.ch
Fri Apr 8 18:50:14 EDT 2005


I see! It's the in-place aspect that led me to assume that the assignment to self inside the method would do the trick. Next the explicit call reinforced the perception. Anyway, thank you very much for the clarification.
Frederic

> 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> 
>
>
>
> -- 
> http://mail.python.org/mailman/listinfo/python-list
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20050409/b8049702/attachment.html>


More information about the Python-list mailing list