simple newbie question

Bengt Richter bokr at oz.net
Tue Dec 10 22:42:16 EST 2002


On Wed, 11 Dec 2002 02:18:39 +0000, eugene kim <eugene1977 at hotmail.com> wrote:

>what's different in these two..assignment, append?
>thank you
>
>listvar1 = [1]
You told Python to:
 (a) create a list object
 (b) create or find an integer object with a value of 1
 (c) put a reference to the integer object in the new list object
 (d) put the name listvar1 in the current local name space and bind it to the list object (a)
>listvar2 = listvar1
 (e) evaluate the expression listvar1, resulting in a reference to the list object created at (a)
 (f) put the name listvar2 in the current local name and bind it to result of (e), which is
     the same list object as the name listvar1 is bound to
>listvar2 = [2]
 (g) create a new list object as in (a,b,c) except with an integer 2 instead of 1
 (h) bind (replacing the previous binding) the listvar2 name to the result of (g)
>
>print listvar1, listvar2 ## [1] [2]
 (i) evaluate the expressions and print their string representations with spaces between and
     a final newline: listvar1 evaluates to what it is bound to ([1]) and listvar2 is bound to [2]
>
>-----------------------------
>
>listvar1 = [1]
 (j) just like (a-d)
>listvar2 = listvar1
 (k) just like (e-f): at this point both listvar1 and listvar2 are bound to the **same** list object
>listvar2.append(2)
 (l) evaluate the whole expression by starting with evaluating listvar2 => a reference to the list
     object whose value is [1] Note that there is no assignment here to rebind listvar2!
 (m) get the attribute 'append' from the specific list object. append is a method of the list type,
     and when you access a class/type attribute via an instance like the object listvar1 is bound to,
     you get back a bound method, which is like a function that is holding on to an object that it
     will operate on/with when you call it as a function. So the result of (m) is a bound method in
     this case spelled <built-in method append of list object at (some hex location)>
 (n) evaluate argument list for the bound method, namely the integer 2
 (o) call the bound method with the argument from (n)
     In this case the bound method is holding on to a reference to the same object that both listvar1
     and listvar2 are bound to (lightbulb ?;-), and append operates to modify the object it is bound to
     by adding the argument (2 here) to the end of the list. So the object is modified to become [1,2],
     but it is the same object, and all previous references to it are still referring to it, so both
     listvar1 and listvar2 were bound to the same object, and they still are, only it's modified.
 (p) If doing this interactively, print the string value returned by the method/function
     if it's not None, but it is None so nothing is printed.
>
>print listvar1, listvar2 ## [1,2][1,2]
 (q) Do as in (i). But here listvar1 and listvar2 are both bound to the same object, so they print
     indentically, whereas listvar2 in the first example became bound to a separate object at (g-h)
     so they evaluated to separate values and printed respective string representations at (i)
>

I could have just pointed you to a FAQ, but I was too lazy too look it up ;-)

Regards,
Bengt Richter



More information about the Python-list mailing list