[Tutor] How to Pass lists by value

Kent Johnson kent37 at tds.net
Tue Dec 6 03:05:11 CET 2005


Hans Dushanthakumar wrote:

> Thanks guys
>  
> Yes either of the foll solves the problem:
>  
> b = junk(copy.copy(a))
>  OR
>
> b = junk(a[:])

These work because they make a copy of a and pass (a reference to) the 
copy to the function.

>   Why is there a difference between the way the two lines 
> (x.append("20")  and x = "30") are handled within a function?

Because they are doing different things. x.append("20") calls a method 
on the object bound to the name x. The method mutates the object, so it 
now has a new value. x = "30" binds the name x to a new object, the 
string "30". This binding happens in the local namespace so it doesn't 
affect the value seen by the caller.

You have to change how you think about variables. In Python, a variable 
is not a storage location into which values are put, it is a reference 
to an object - a name given to an object. Assignment binds a name to a 
value.

When you call a function, the names of the formal parameters are bound 
to the values passed in to the function. Parameters are *always* passed 
by reference. The names inside the function are bound to the same 
objects as the names outside the function, so if you call a mutating 
method on the object, like list.append(), the change will be seen 
externally. If you rebind the name inside the function, this has no 
effect on the bindings external to the function.

I hope this helps. There is a fundamental shift in the way you think 
about names that is needed to grok Python. Once you get it much will 
become clearer - The Zen of Python says, "Namespaces are one honking 
great idea -- let's do more of those!" so it's important to understand 
namespaces :-)

This may help:
http://effbot.org/zone/python-objects.htm

Kent

>
>  
> ------------------------------------------------------------------------
> *From:* Adam [mailto:adam.jtm30 at gmail.com]
> *Sent:* Tuesday, 6 December 2005 1:49 p.m.
> *To:* Hans Dushanthakumar
> *Subject:* Re: [Tutor] How to Pass lists by value
>
> On 06/12/05, *Hans Dushanthakumar* <Hans.Dushanthakumar at navman.com 
> <mailto:Hans.Dushanthakumar at navman.com>> wrote:
>
>
>     Hi folks,
>        How do I pass a list by value to a function.
>
>     The foll: snippet of code produces the output as shown:
>
>     Code:
>     -----
>     def junk(x):
>         x.append("20")
>         return x
>
>     a = ["10"]
>     b = junk(a)
>
>     print b
>     print a
>
>
>     Output:
>     -------
>>>>
>     b =  ['10', '20']
>     a =  ['10', '20']
>
>     This indicates that the variable "a" was passed by reference
>     because the
>     function alters its value in the main script.
>
>     However, the following code produces a slightly diff output:
>
>     Code:
>     -----
>
>     def junk(x):
>         x = "30"
>         return x
>
>     a = ["10"]
>     b = junk(a)
>
>     print "b = ", b
>     print "a = ", a
>
>     Output:
>     -------
>>>>
>     b =  30
>     a =  ['10']
>
>     In this case, "a" seems to be passed by value, because the value is
>     unchanged even after the call to the function.
>
>
>     Question is, in the 1st scenario, how do I forcefully pass the
>     variable
>     by value rather than reference. Why is there a difference between the
>     two scenarios?
>
>     Cheers
>     Hans
>     _______________________________________________
>     Tutor maillist  -  Tutor at python.org <mailto:Tutor at python.org>
>     http://mail.python.org/mailman/listinfo/tutor
>     <http://mail.python.org/mailman/listinfo/tutor>
>
>
>  
> def junk(x):
>    x.append("20")
>    return x
>
> a = ["10"]
> b = junk(a[:])
>
> That should give you what you're looking for a[:] will pass the values 
> of a.
>
>------------------------------------------------------------------------
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
>  
>




More information about the Tutor mailing list