no pass-values calling?
Steven D'Aprano
steven at REMOVE.THIS.cybersource.com.au
Wed Jan 16 01:30:27 EST 2008
On Wed, 16 Jan 2008 13:59:03 +0800, J. Peng wrote:
> Hi,
>
> How to modify the array passed to the function? I tried something like
> this:
>
>>>> a
> [1, 2, 3]
>>>> def mytest(x):
> ... x=[4,5,6]
This line does NOT modify the list [1, 2, 3]. What it does is create a
new list, and assign it to the name "x". It doesn't change the existing
list.
If you have not already done this, you should read this:
http://effbot.org/zone/python-objects.htm
Consider this function:
def test(alist):
alist.append(0) # this modifies the existing list
alist = [1, 2, 3] # this changes the name "alist"
return alist
Now try it:
oldlist = [10, 9, 8, 7]
newlist = test(oldlist)
Can you predict what oldlist and newlist will be equal to?
oldlist will be [10, 9, 8, 7, 0] and newlist will be [1, 2, 3]. Do you
see why?
--
Steven
More information about the Python-list
mailing list