simple newbie question

maney at pobox.com maney at pobox.com
Tue Dec 10 22:11:52 EST 2002


eugene kim <eugene1977 at hotmail.com> wrote:
> what's different in these two..assignment, append?
> thank you
> 
> listvar1 = [1]

listvar1 is bound to the list [1]

> listvar2 = listvar1

listvar2 is bound to the same list [1] 

> listvar2 = [2]

listvar2 is unbound from the list [1] and bound to the separate list [2]

> print listvar1, listvar2 ## [1] [2]

> -----------------------------

> listvar1 = [1]

listvar1 is bound to the list [1]

> listvar2 = listvar1

listvar2 is bound to the same list [1] 

> listvar2.append(2)

2 is appended to the list [1] to which listvar2 is still bound; this
modifies the existing list rather than creating a new one and binding
the latter to listvar2

> print listvar1, listvar2 ## [1,2][1,2]

QED.  :-)

Try this with other third statements; compare and contrast

listvar2 = listvar2 + [2]

listvar2 += [2]




More information about the Python-list mailing list