Problem with assigning variables of type List

Peter Hansen peter at engcorp.com
Tue Aug 20 07:34:13 EDT 2002


Paul Foley wrote:
> 
> On Tue, 20 Aug 2002 09:27:02 +0200, Max M wrote:
> > 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.
> 
> 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.

Nope.  Assignment works by rebinding the name to something else,
in effect changing the reference.  There is no pass by value in 
Python.  (Although somebody may still refute this successfully,
but I don't think you have so far.)

Maybe the issue is that these conventional terms do not apply
to Python as well as they do to more conventional languages...

-Peter



More information about the Python-list mailing list