[Python-Dev] Split augmented assignment into two operator sets? [Re: SyntaxError: can't assign to function call]

Aahz aahz at pythoncraft.com
Fri Aug 11 06:23:07 CEST 2006


On Fri, Aug 11, 2006, Greg Ewing wrote:
> Phillip J. Eby wrote:
>> 
>> I think it's a warning sign because I *know* what augmented
>> assignment's semantics are supposed to be and I *still* try to use it
>> with setdefault() sometimes -- but only when I'm doing an augmented
>> assignment.  I never mistakenly try to assign to a function call in
>> any other circumstance.
>
> I think this problem arises because of the dual semantics of augmented
> assignment. One tends to have two *different* mental models of what it
> does, depending on whether the object in question is immutable:
>
>    (1) For immutable objects:
> 
>       x += y  <-->  x = x + y
> 
>    (2) For mutable objects:
> 
>       x += y  <-->  x.__iadd__(y)

What I try to do is always remember that

    x += y  <-->  x = x.__iadd__(y)

which mostly solves the problem.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it."  --Brian W. Kernighan


More information about the Python-Dev mailing list