no pass-values calling?

Chris cwitts at gmail.com
Wed Jan 16 01:33:03 EST 2008


On Jan 16, 7:59 am, "J. Peng" <peng.... at gmail.com> wrote:
> On Jan 16, 2008 1:45 PM, Dennis Lee Bieber <wlfr... at ix.netcom.com> wrote:
>
> > On Wed, 16 Jan 2008 11:09:09 +0800, "J. Peng" <peng.... at gmail.com>
>
> > alist = []
> > anint = 2
> > astr = "Touch me"
>
> > dummy(alist, anint, astr)
>
> >         "dummy" can only modify the contents of the first argument -- the
> > integer and string can not be mutated.
>
> 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]
> ...>>> mytest(a)
> >>> a
>
> [1, 2, 3]
>
> As you see, a was not modified.
> Thanks!

'a' was not modified because you locally assigned a new object with
'x=[4,5,6]'.  If you want the new list you created you will have to
return it.  You can see how you modify it if you were to use
'x.append()' or 'x.extend()' for eg.



More information about the Python-list mailing list