How can this be?

Paul Prescod paul at prescod.net
Thu Feb 5 20:41:20 EST 2004


r.e.s. wrote:

> My problem is this ...
> 
> Module A contains the definition of a function f.
> Module B contains:
> 
>     from A import *
>     L = [0]
>     print L
>     x = f(L, 'a data string')
>     print L
 > ...
> 
> How can the list L have gotten changed??

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.

  Paul Prescod






More information about the Python-list mailing list