confusion about variable scope in a class

Diez B. Roggisch deets at nospam.web.de
Sat Feb 14 11:37:07 EST 2009


gyro schrieb:
> Hi,
> I was writing a Python script to perform some data analyses and was
> surprised by some behavior I noted. A simple test program illustrating
> the behavior is below.
> I do not understand why the value of 'data' is being modified. I am
> obviously missing something obvious, and would certainly appreciate an
> explanation of why this is happening.


Lists are mutables, and python passes around references to objects, not 
copies.

So if you want a copy, you need to create one explicit, either by using e.g.


new_list = list(old_list)

which will create a shollow copy of the list, or by using the 
library-module "copy" that also offers a "deepcopy" function.


Diez



More information about the Python-list mailing list