Trying to understand += better

Roy Smith roy at panix.com
Sun Nov 22 23:38:28 EST 2009


In article <4b0a01aa$1 at dnews.tpgi.com.au>, Lie Ryan <lie.1296 at gmail.com> 
wrote:

> The semantic of the in-place operator is something like:
> x += y
> becomes
> x = x.__iadd__(y)
> 
> thus
> foo.bar += baz
> becomes
> foo.bar = foo.bar.__iadd__(baz)
> 
> So the call sequence is,
> foo.__getattr__('bar') ==> x
> x.__iadd__(baz) ==> y
> foo.__setattr__('bar', y)

I don't get where the __setattr__() call comes from in this situation.  I 
thought the whole idea of __iadd__(self, other) is that it's supposed to 
mutate self.  So, why is there another assignment happening after the 
__iadd__() call?



More information about the Python-list mailing list