understanding list scope

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alex

    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
  • George Sakkis

    #2
    Re: understanding list scope

    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

    Comment

    • alex23

      #3
      Re: understanding list scope

      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_s et)
      >>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_s et)
      >>ds[1]['param'] = "y"
      >>ds[2]['param'] = "x"


      Comment

      • Alex

        #4
        Re: understanding list scope

        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!

        Comment

        • Peter Pearson

          #5
          Re: understanding list scope

          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.

          Comment

          • mbezzi@landi.it

            #6
            Re: understanding list scope

            On Sep 21, 7:48 pm, Peter Pearson <ppear...@nowhe re.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

            Comment

            Working...