understanding list scope

A

Alex

Hi all!

I have a problem understanding the behaviour of this snippet:

data_set = ({"param":"a"},{"param":"b"},{"param":"c"})

for i in range(len(data_set)):
ds = data_set[:]
data = ds
if i == 1: data['param'] = "y"
if i == 2: data['param'] = "x"

print data_set


This script print out:
({'param': 'a'}, {'param': 'y'}, {'param': 'x'})

Why? I'm coping data_set in ds so why data_set is changed?

Thanks in advance.

Alex
 
G

George Sakkis

Hi all!

I have a problem understanding the behaviour of this snippet:

data_set = ({"param":"a"},{"param":"b"},{"param":"c"})

for i in range(len(data_set)):
    ds = data_set[:]
    data = ds
    if i == 1: data['param'] = "y"
    if i == 2: data['param'] = "x"

print data_set

This script print out:
({'param': 'a'}, {'param': 'y'}, {'param': 'x'})

Why? I'm coping data_set in ds so why data_set is changed?


Because you're doing a shallow copy: http://docs.python.org/lib/module-copy..html

George
 
A

alex23

Why? I'm coping data_set in ds so why data_set is changed?

You're making a copy of the ds tuple, which has the -same- contents as
the original. To create copies of the contents as well, try the
deepcopy function from the copy module.

As an aside, you're also trying to make a copy of ds for each
iteration of the loop, which is unnecessary in this case. Here's a
slightly better example of your code:
.... if i == 1: data['param'] = "y"
.... if i == 2: data['param'] = "x"
....({'param': 'a'}, {'param': 'y'}, {'param': 'x'})

Although your use of a tuple full of dicts for data_set is kinda
strange... Tuples are generally used when you want a structured data
element, in which case you'd just address each element directly rather
than iterate through it:
ds = deepcopy(data_set)
ds[1]['param'] = "y"
ds[2]['param'] = "x"
 
A

Alex

I have a problem understanding the behaviour of this snippet:
data_set = ({"param":"a"},{"param":"b"},{"param":"c"})
for i in range(len(data_set)):
ds = data_set[:]
data = ds
if i == 1: data['param'] = "y"
if i == 2: data['param'] = "x"

print data_set
This script print out:
({'param': 'a'}, {'param': 'y'}, {'param': 'x'})
Why? I'm coping data_set in ds so why data_set is changed?

Because you're doing a shallow copy:http://docs.python.org/lib/module-copy.html

George


Thanks a lot. It was giving me and headache!
 
P

Peter Pearson

I have a problem understanding the behaviour of this snippet:
[snip]
Because you're doing a shallow copy:
http://docs.python.org/lib/module-copy.html
[snip]
Thanks a lot. It was giving me and headache!

FWIW, since I started following this newsgroup, I've noticed
that I no longer have those crises that revolve around the depth
of a copy. I conjecture that they resulted from non-pythonic
style. Try following this newsgroup for a while, and you might
see a lot of startlingly elegant ways of doing things.
 
M

mbezzi

FWIW, since I started following this newsgroup, I've noticed
that I no longer have those crises that revolve around the depth
of a copy.  I conjecture that they resulted from non-pythonic
style.  Try following this newsgroup for a while, and you might
see a lot of startlingly elegant ways of doing things.

I know the code is really ugly but I just make a quick and dirty
snippet to ask for help.
I should have wrapped the code between <bad code></bas code> tags ;-)

Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top