473,287 Members | 3,253 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

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 779
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: ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.