lists: += vs. .append() & oddness with scope of variables

Scott David Daniels scott.daniels at
Sun Mar 5 17:34:15 CET 2006

Duncan Booth wrote:
> Sandro Dentella wrote:
>> I'd like to understand why += operator raises an error while .append()
>> does not. My wild guess is the parses treats them differently but I
>> cannot understand why this depends on scope of the variables (global
>> or class variables):
> Any assignment to a variable within a function means that the name to which 
> you are assigning is regarded as a local variable (unless you use the 
> 'global' statement to override that). += is a form of assignment, calling 
> the append method is not an assignment.
> The solution here is simply to use 'global a' to tell the compiler that you 
> meant to assign the the global variable rather than creating a new local 
> variable.

As Duncan knows but forgot to mention, eric.append(spam) doesn't write
the variable eric, it simply manipulates the object that eric names.

-Scott David Daniels
scott.daniels at

More information about the Python-list mailing list