list operation '+= ' or assignment problem

Fredrik Lundh fredrik at effbot.org
Fri Jan 5 03:14:15 EST 2001


Aahz Maruch wrote:
> No, but I'll explain why some people are saying "yes":
>
> foo = "abc"
> bar = foo
> bar += "d"
>
> bar now contains a reference to "abcd", which is a copy of the original
> string "abc" plus the additional letter "d".

I think it's more because Joe used "assignment" to refer to
both ordinary assignment and augmented assignment.

In Python, the latter is really just syntax sugar for a method
call, while the former is an operation on the namespace, not
the object.

The method call may make a copy, if it prefers to.  The name-
space operation never makes any copies, for any kind of object.

(But you already knew that, of course)

Cheers /F







More information about the Python-list mailing list