How can this be?

r.e.s. r.s at ZZmindspring.com
Thu Feb 5 21:23:38 EST 2004


"Paul Prescod" <paul at prescod.net> wrote ...

> You sent a reference to L into the function. The function probably 
> mutated it. If you wish the function to work with a copy, try this:
> 
>       from A import *
>       L = [0]
>       print L
>       x = f(L[:], 'a data string')
>       print L
> 
> This is normal and often useful behaviour.

I can see that it would be -- Thanks for answering 
a beginner's question. I notice also the following:

    def f(L): 
        L[0] = 1
    
    def g(L):
        L = [1]

    L1 = [0],  L2 = [0]

    f(L1), g(L2)

... L1 gets mutated, but not L2 (even though both
references were passed).  It's probably explained 
in the tutorial -- which I'm now going to re-read.

--r.e.s.



More information about the Python-list mailing list