List sequential initialization

René Fleschenberg rene at korteklippe.de
Tue Jun 12 14:04:29 EDT 2007


HMS Surprise schrieb:
> I thought if I could do this:
> >>> a = b = ''

Bind both the names a and b to the same string object.

> >>> a = 'a'

Bind the name a to a *new* string object with the value 'a'. This
replaces the previous binding of the name a.

> >>> la = lb = []

Bind both the names la and lb to the same list object.

> >>> la.append('a')

Append the string 'a' to the *existing* list object referenced by the
name la. This modifies the object that is referenced by the name la (and
also by the name lb), it does not create a new object. The equivalent to
what you did with the strings above would have been:

	la = ['a']

This would have created a new list object and bound the name la to it,
while the name lb would still reference the other list object you
created earlier.

If you want to modify the list referenced by la but not the one
referenced by lb, you need to actually *copy* the list with one of these
methods:

	lb = list(la)

or

	lb = la[:]

-- 
René



More information about the Python-list mailing list