understanding list scope

Discussion in 'Python' started by Alex, Sep 21, 2008.

  1. Alex

    Alex Guest

    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
    Alex, Sep 21, 2008
    #1
    1. Advertising

  2. On Sep 21, 8:51 am, Alex <> wrote:
    > 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
    George Sakkis, Sep 21, 2008
    #2
    1. Advertising

  3. Alex

    alex23 Guest

    On Sep 21, 10:51 pm, Alex <> wrote:
    > 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"
    alex23, Sep 21, 2008
    #3
  4. Alex

    Alex Guest

    On 21 Set, 15:07, George Sakkis <> wrote:
    > On Sep 21, 8:51 am, Alex <> wrote:
    >
    >
    >
    > > 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


    Thanks a lot. It was giving me and headache!
    Alex, Sep 21, 2008
    #4
  5. On Sun, 21 Sep 2008 06:17:36 -0700 (PDT), Alex wrote:
    > On 21 Set, 15:07, George Sakkis <> wrote:
    >> On Sep 21, 8:51 am, Alex <> wrote:

    [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.
    Peter Pearson, Sep 21, 2008
    #5
  6. Alex

    Guest

    On Sep 21, 7:48 pm, Peter Pearson <> wrote:

    > 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 code> tags ;-)

    Alex
    , Sep 22, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Opal
    Replies:
    12
    Views:
    948
    Paul Opal
    Oct 11, 2004
  2. Pyenos
    Replies:
    9
    Views:
    291
    WaterWalk
    Dec 29, 2006
  3. Prabh

    Understanding 'scope'

    Prabh, Feb 15, 2004, in forum: Perl Misc
    Replies:
    7
    Views:
    123
    robic0
    Nov 29, 2005
  4. Roger

    Understanding Scope with 'my'

    Roger, May 17, 2004, in forum: Perl Misc
    Replies:
    2
    Views:
    110
    Roger
    May 18, 2004
  5. Andrew Falanga
    Replies:
    2
    Views:
    201
    Andrew Falanga
    Nov 22, 2008
Loading...

Share This Page