Bug or feature?

James Henderson james at logicalprogression.net
Sat Jan 17 01:51:05 CET 2004


On Friday 16 January 2004 10:51 pm, Skip Montanaro wrote:
>     >> Of course, it should be noted that, in Python, "a += b" is only
>     >> sometimes synonymous with "a = a + b".  The rest of the time, it's
>     >> hard to say what it is synonymous with :)
>
>     James> What do you have in mind?  J.
>
> For immutable objects, += works as you'd expect: return a new object,
> leaving the old object unchanged.  That's not the case for mutable objects,
>
> to wit:
>     >>> foo = [1]
>     >>> bar = foo
>     >>> foo += [2]
>
> The object referenced by foo is modified in-place...
>
>     >>> bar
>
>     [1, 2]
>
>     >>> foo = foo + [2]
>
> Here foo is bound to a new object, leaving the old object (still referenced
> by bar) unchanged.
>
>     >>> foo
>
>     [1, 2, 2]
>
>     >>> bar
>
>     [1, 2]
>
> Skip

Thanks for pointing that out.  It's also the case the += may be more 
efficient, although that doesn't undermine my original point about surface 
behaviour.

For any third party interested the details are at:

http://www.python.org/doc/current/ref/augassign.html

Back to the original topic, I have to agree with Robert Brewer that Alexey 
Nezhdanov's proposal was capable of several other interpretations than a 
switch to right-to-left evaluation.

James
-- 
James Henderson, Logical Progression Ltd.
http://www.logicalprogression.net/
http://sourceforge.net/projects/mailmanager/





More information about the Python-list mailing list