Static variable vs Class variable

paul.melis at gmail.com paul.melis at gmail.com
Wed Oct 17 05:20:30 EDT 2007


On Oct 17, 11:08 am, Duncan Booth <duncan.bo... at invalid.invalid>
wrote:
> paul.me... at gmail.com wrote:
> > Curious, do you have the relevant section in the docs that describes
> > this behaviour?
>
> Yes, but mostly by implication. In section 3.4.7 of the docs, the sentence
> before the one you quoted says:
>
>   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).
>
> The 'does not have to be self' tells you that the result of __iadd__ is
> used, i.e there is still an assignment going on.
>
> Just read all of that paragraph carefully. It says that if there is no
> __iadd__ method it considers calling __add__/__radd__. Nowhere does it say
> that it handles the result of calling the methods differently.

Right, the paragraph is actually pretty clear after a second reading.
I find it surprising nonetheless, as it's easy to forget to return a
result when you're implementing a method that does an in-place
operation, like __iadd__:

>>> class C:
...     def __init__(self, v):
...             self.v = v
...     def __iadd__(self, other):
...             self.v += other
...
>>> c=C(1)
>>> c.v
1
>>> c += 3
>>> c
>>> c is None
True


Paul




More information about the Python-list mailing list