Problem with assigning variables of type List

Max M maxm at mxm.dk
Tue Aug 20 03:27:02 EDT 2002


Paul Foley wrote:


> On 19 Aug 2002 14:48:34 -0700, Abhishek Roy wrote:


>>Thank you very much. I had not realized that lists are passed by
>>reference by default,


> Good.  Because they're not.

Please elaborate with an example. A statement like that dosn't add much 
to the discussion.

But I don't think we have the same definition of "by reference" and "by 
value"!

l1 = l2 = [1,2]
l1.append(3)
print l2
 >>>[1, 2, 3]

That is "by reference" in my book.


 From the docs:

3.1 Objects, values and types
Types affect almost all aspects of object behavior. Even the importance 
of object identity is affected in some sense: for immutable types, 
operations that compute new values may actually return a reference to 
any existing object with the same type and value, while for mutable 
objects this is not allowed. E.g., after "a = 1; b = 1", a and b may or 
may not refer to the same object with the value one, depending on the 
implementation, but after "c = []; d = []", c and d are guaranteed to 
refer to two different, unique, newly created empty lists. (Note that "c 
= d = []" assigns the same object to both c and d.)



regards Max M




More information about the Python-list mailing list