After reading http://www.effbot.org/zone/python-objects.htm I am left with this question:
In python, if a=1
creates an integer-object and binds it to the name a
, b=[]
creates an empty list-object and binds it to name b
, what happens when I call e.g. c=[1]
?
I suppose this creates a list-object and binds it to the name c
, but how is the 1
handled exactly? What does the actual content of the list-object look like, under the hood? Does it consist of an integer-object or a reference to a "separate" integer-object? Is it okay to think of e.g. c[0]
as a name bound to a list item?
And what about the following:
d=1 # creates int(1)-object and binds it to d
e=[d] # creates list-object and binds it to e, but what happens with d?
Will the content of the list-object (named e
) be a reference to the integer-object called d
or a new integer-object?
I guess the answer lies in this quote from Mr. Lundh, from the source mentioned above, but I am still a tiny bit confused:
You’re then calling a method on that object, telling it to append an integer object to itself. This modifies the content of the list object, but it doesn’t touch the namespace, and it doesn’t touch the integer object.
Also I believe part of the answer is found here: Python; name bindings are not object references?, but I am still looking for some more insight.