473,471 Members | 1,684 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

understanding list scope

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[i]
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
Sep 21 '08 #1
5 786
On Sep 21, 8:51*am, Alex <metallourla...@gmail.comwrote:
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[i]
* * 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
Sep 21 '08 #2
On Sep 21, 10:51*pm, Alex <metallourla...@gmail.comwrote:
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:
>>from copy import deepcopy
data_set = ({"param":"a"},{"param":"b"},{"param":"c"})
ds = deepcopy(data_set)
for i, data in enumerate(ds):
.... if i == 1: data['param'] = "y"
.... if i == 2: data['param'] = "x"
....
>>print data_set
({'param': 'a'}, {'param': 'b'}, {'param': 'c'})
>>print ds
({'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"


Sep 21 '08 #3
On 21 Set, 15:07, George Sakkis <george.sak...@gmail.comwrote:
On Sep 21, 8:51 am, Alex <metallourla...@gmail.comwrote:
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[i]
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!
Sep 21 '08 #4
On Sun, 21 Sep 2008 06:17:36 -0700 (PDT), Alex wrote:
On 21 Set, 15:07, George Sakkis <george.sak...@gmail.comwrote:
>On Sep 21, 8:51 am, Alex <metallourla...@gmail.comwrote:
[snip]
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.

--
To email me, substitute nowhere->spamcop, invalid->net.
Sep 21 '08 #5
On Sep 21, 7:48*pm, Peter Pearson <ppear...@nowhere.invalidwrote:
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.

--
To email me, substitute nowhere->spamcop, invalid->net.
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 codetags ;-)

Alex
Sep 22 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: caw | last post by:
this is the smallest bit of code I could get to demonstrate what I want to understand... def msg(text): def decorate(f): def new_f(*args): print text, f(*args) return new_f return decorate
9
by: kazio | last post by:
Hello, So, I need to have double linked, circular list (last element point to the first one and first one points to the last one). I thought maybe I could use list container from STL, but...
2
by: lastusernameleft | last post by:
Is there a .NET method for doing this? I haven't found anything else that works. Thanks
6
by: ahart | last post by:
I'm pretty new to python and am trying to write a fairly small application to learn more about the language. I'm noticing some unexpected behavior in using lists in some classes to hold child...
9
by: Rudy | last post by:
Hello All! I'm a little confused on Public Class or Modules. Say I have a this on form "A" Public Sub Subtract() Dim Invoice As Decimal Dim Wage As Decimal Static PO As Decimal Invoice =...
3
by: Pantokrator | last post by:
Hi all, I've got a question about the scope of the constructor initialisation list. First of all, let me explain my classes: // ***************************************************** class...
1
by: Bharath | last post by:
Hello everyone, I was going through templates section at Bjarne Stroustrup's "C++ Programming Language" (13.2.1 Defining a Template ). I couldn't understand some part of it. Need your help in...
13
by: Chris Carlen | last post by:
Hi: I have begun learning Python by experimenting with the code snippets here: http://hetland.org/writing/instant-python.html In the section on functions, Magnus Lie Hetland writes: ...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.