Problem with assigning variables of type List

Max M maxm at mxm.dk
Tue Aug 20 08:31:51 EDT 2002


> Paul Foley wrote:
> 
>>Try this:
>>
>>  >>> def reftest(x):
>>  ...    x = [42]
>>  ...
>>  >>> y = [1,2,3]
>>  >>> reftest(y)
>>  >>> y
>>  [1, 2, 3]
>>
>>if it were passed by reference, you'd see [42] on the last line.


your example corresponds to:

 >>> x = y = [1,2,3]
 >>> x = [42]
 >>> y
[1,2,3]

what happens is that the 'name' x points to, or reference, [42] instead 
of [1,2,3]


regards Max M




More information about the Python-list mailing list