Lists & "pointers"

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

    Lists & "pointers"

    Hello all,

    I have written a simple whiteboard application. In my application, I
    want to be able to set draw attributes. This part works. I have a
    dictionary object which contains stuff like:
    self.attr['Pen.Color'] = ...
    self.attr['Pen.Thickness'] = ...

    Now, the problem is that I want to be able to store attributes in a
    list so they'll be easily accessed using the function keys. I.e. I have
    the "current attributes" which I want to be able to store or retrieve
    in/from a list,

    The problem is that I have initialized the list like this:

    self.drawAttr = { blah, blah, blah.. }
    self.storedAttr = [ ]
    for i in range(0, 10):
    self.storedAttr .append(self.dr awAttr)

    I know what the problem is; they are all referencing the *same*
    dictionary object. So, my question is: How do I initialize a list of
    dictionary objects, where each list entry is its own object (which is a
    copy from the self.drawAttr object).

    Also, how do I store/restore entries to the list?

    I have found the "copy" module, and it's copy method. I assume this
    would work:

    for i in range(0, 10):
    self.storedAttr .append(copy.co py(self.drawAtt r))

    However, the concept of "deep copy" confuses me. Do I want it, or
    don't I want it? I repeat: the attributes object is a simple dictionary.

    Thankful for any advice.
  • rixil@hotmail.com

    #2
    Re: Lists & "pointers& quot;

    Jan Danielsson wrote:[color=blue]
    > Hello all,
    >
    > I have written a simple whiteboard application. In my application, I
    > want to be able to set draw attributes. This part works. I have a
    > dictionary object which contains stuff like:
    > self.attr['Pen.Color'] = ...
    > self.attr['Pen.Thickness'] = ...
    >
    > Now, the problem is that I want to be able to store attributes in a
    > list so they'll be easily accessed using the function keys. I.e. I have
    > the "current attributes" which I want to be able to store or retrieve
    > in/from a list,
    >
    > The problem is that I have initialized the list like this:
    >
    > self.drawAttr = { blah, blah, blah.. }
    > self.storedAttr = [ ]
    > for i in range(0, 10):
    > self.storedAttr .append(self.dr awAttr)
    >
    > I know what the problem is; they are all referencing the *same*
    > dictionary object. So, my question is: How do I initialize a list of
    > dictionary objects, where each list entry is its own object (which is a
    > copy from the self.drawAttr object).
    >
    > Also, how do I store/restore entries to the list?
    >
    > I have found the "copy" module, and it's copy method. I assume this
    > would work:
    >
    > for i in range(0, 10):
    > self.storedAttr .append(copy.co py(self.drawAtt r))
    >
    > However, the concept of "deep copy" confuses me. Do I want it, or
    > don't I want it? I repeat: the attributes object is a simple dictionary.
    >
    > Thankful for any advice.[/color]

    The easiest way to do it would be to create a new dictionary object for
    each iteration of your loop. In this scenario, you would not need to
    use the copy module.

    In other words:

    self.storedAttr = [ ]
    for i in range(0, 10):
    self.storedAttr .append({ blah, blah, blah.. })

    I hope this helps!

    Regards,

    Michael Loritsch

    Comment

    • George Sakkis

      #3
      Re: Lists & "pointers& quot;

      <rixil@hotmail. com> wrote:
      [color=blue]
      > Jan Danielsson wrote:[color=green]
      > > Hello all,
      > >
      > > I have written a simple whiteboard application. In my application, I
      > > want to be able to set draw attributes. This part works. I have a
      > > dictionary object which contains stuff like:
      > > self.attr['Pen.Color'] = ...
      > > self.attr['Pen.Thickness'] = ...
      > >
      > > Now, the problem is that I want to be able to store attributes in a
      > > list so they'll be easily accessed using the function keys. I.e. I have
      > > the "current attributes" which I want to be able to store or retrieve
      > > in/from a list,
      > >
      > > The problem is that I have initialized the list like this:
      > >
      > > self.drawAttr = { blah, blah, blah.. }
      > > self.storedAttr = [ ]
      > > for i in range(0, 10):
      > > self.storedAttr .append(self.dr awAttr)
      > >
      > > I know what the problem is; they are all referencing the *same*
      > > dictionary object. So, my question is: How do I initialize a list of
      > > dictionary objects, where each list entry is its own object (which is a
      > > copy from the self.drawAttr object).
      > >
      > > Also, how do I store/restore entries to the list?
      > >
      > > I have found the "copy" module, and it's copy method. I assume this
      > > would work:
      > >
      > > for i in range(0, 10):
      > > self.storedAttr .append(copy.co py(self.drawAtt r))
      > >
      > > However, the concept of "deep copy" confuses me. Do I want it, or
      > > don't I want it? I repeat: the attributes object is a simple dictionary.
      > >
      > > Thankful for any advice.[/color]
      >
      > The easiest way to do it would be to create a new dictionary object for
      > each iteration of your loop. In this scenario, you would not need to
      > use the copy module.
      >
      > In other words:
      >
      > self.storedAttr = [ ]
      > for i in range(0, 10):
      > self.storedAttr .append({ blah, blah, blah.. })[/color]

      And this would be equivalent to shallow copy. Whether you need a deep copy depends on what each
      "blah" is. More specifically it depends on whether the values of the dictionary are mutable or not
      (the keys are known to be immutable anyway). If they are immutable, a shallow copy is enough. If
      not, check whether all dictionaries refer to the same values or separate copies of the values. Only
      in the latter case you need deep copy.

      HTH,

      George


      Comment

      • Kay Schluehr

        #4
        Re: Lists &amp; &quot;pointers& quot;

        Jan Danielsson wrote:[color=blue]
        > Hello all,
        >
        > I have written a simple whiteboard application. In my application, I
        > want to be able to set draw attributes. This part works. I have a
        > dictionary object which contains stuff like:
        > self.attr['Pen.Color'] = ...
        > self.attr['Pen.Thickness'] = ...
        >
        > Now, the problem is that I want to be able to store attributes in a
        > list so they'll be easily accessed using the function keys. I.e. I have
        > the "current attributes" which I want to be able to store or retrieve
        > in/from a list,
        >
        > The problem is that I have initialized the list like this:
        >
        > self.drawAttr = { blah, blah, blah.. }
        > self.storedAttr = [ ]
        > for i in range(0, 10):
        > self.storedAttr .append(self.dr awAttr)
        >
        > I know what the problem is; they are all referencing the *same*
        > dictionary object. So, my question is: How do I initialize a list of
        > dictionary objects, where each list entry is its own object (which is a
        > copy from the self.drawAttr object).[/color]

        Hi Jan son of Daniel,

        you might initialize self.storedAttr with empty dicts and fill them
        later:

        self.soredAttr = [{}]*10
        for entry in self.storedAttr :
        entry.update(se lf.drawAttr)

        [color=blue]
        > Also, how do I store/restore entries to the list?
        >
        > I have found the "copy" module, and it's copy method. I assume this
        > would work:
        >
        > for i in range(0, 10):
        > self.storedAttr .append(copy.co py(self.drawAtt r))
        >
        > However, the concept of "deep copy" confuses me. Do I want it, or
        > don't I want it? I repeat: the attributes object is a simple dictionary.
        >
        > Thankful for any advice.[/color]

        A *shallow copy* creates a new dictionary and copies the references, a
        *deep copy* tries to create a new reference for each existing object in
        the dict. The disadvantage of deepcopy is that it does not work in many
        cases:
        [color=blue][color=green][color=darkred]
        >>> copy.deepcopy([lambda :None])[/color][/color][/color]
        Traceback (most recent call last):
        ....
        TypeError: function() takes at least 2 arguments (0 given)

        As the docs tell:

        "This version does not copy types like module, class, function, method,
        stack trace, stack frame, file, socket, window, array, or any similar
        types."

        I wonder if one couldn't pickle a module and reimport it in order to
        create a copy of it ;)

        IMO this is a weakness of the algorithm. One usually doesn't want to
        duplicate a function so that a new reference of a function is not
        needed because it is readonly and the algorithm could reuse the same
        reference. For classes I don't if the assertion in the docs is actually
        true?
        [color=blue][color=green][color=darkred]
        >>> class A:pass
        >>> copy.deepcopy(A )[/color][/color][/color]
        <class __main__.A at 0x01191990>
        [color=blue][color=green][color=darkred]
        >>> class A(object):[/color][/color][/color]
        .... def __init__(self): pass
        ....[color=blue][color=green][color=darkred]
        >>> copy.deepcopy(A )[/color][/color][/color]
        <class '__main__.A'>[color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]

        Regards,
        Kay

        Comment

        • André Malo

          #5
          Re: Lists &amp; &quot;pointers& quot;

          * Kay Schluehr wrote:
          [color=blue]
          > you might initialize self.storedAttr with empty dicts and fill them
          > later:
          >
          > self.soredAttr = [{}]*10
          > for entry in self.storedAttr :
          > entry.update(se lf.drawAttr)[/color]

          As a matter of fact, you're doing the same ;-)

          In [1]: x = [{}] * 10

          In [2]: x[0]['a'] = 1

          In [3]: x[1]['b'] = 2

          In [4]: x
          Out[4]:
          [{'a': 1, 'b': 2},
          {'a': 1, 'b': 2},
          {'a': 1, 'b': 2},
          {'a': 1, 'b': 2},
          {'a': 1, 'b': 2},
          {'a': 1, 'b': 2},
          {'a': 1, 'b': 2},
          {'a': 1, 'b': 2},
          {'a': 1, 'b': 2},
          {'a': 1, 'b': 2}]

          nd
          --
          "Umfassende s Werk (auch fuer Umsteiger vom Apache 1.3)"
          -- aus einer Rezension

          <http://pub.perlig.de/books.html#apac he2>

          Comment

          • Steven D'Aprano

            #6
            Re: Lists &amp; &quot;pointers& quot;

            On Sat, 23 Jul 2005 17:03:08 +0200, Jan Danielsson wrote:
            [color=blue]
            > The problem is that I have initialized the list like this:
            >
            > self.drawAttr = { blah, blah, blah.. }
            > self.storedAttr = [ ]
            > for i in range(0, 10):
            > self.storedAttr .append(self.dr awAttr)
            >
            > I know what the problem is; they are all referencing the *same*
            > dictionary object. So, my question is: How do I initialize a list of
            > dictionary objects, where each list entry is its own object (which is a
            > copy from the self.drawAttr object).[/color]

            self.drawAttr = { blah, blah, blah.. }
            self.storedAttr = [ ]
            for i in range(0, 10):
            self.storedAttr .append(self.dr awAttr.copy())

            You only need to worry about the difference between copy and deepcopy if
            the objects inside the dict are complex objects like dicts and lists.

            You also said that: "I want to be able to store attributes in a list so
            they'll be easily accessed using the function keys."

            I don't think this is good usage. What happens when you change the
            attributes in one place but forget to change it in the other?

            A better solution would be to set up either a list or a mapping from
            function key to attribute, rather than to a COPY of the attribute. Why
            change things in two places rather than one?

            Something like this:

            # set up attributes before hand
            self.attr['Pen.Color'] = 'blue'
            self.attr['Pen.Thickness'] = 1
            self.attr['Pen.State'] = 'down'
            # etc
            # now point the function keys to attributes
            self.functionke ys = {'F1' = 'Pen.Color', 'F2' = 'Pen.Thickness' ,
            'F3' = 'Pen.State', ... }

            Then, when you want to access the current value of some attribute, instead
            of looking up a list:

            # bad way
            def get_attribute(f key):
            if fkey = 'F1':
            return self.storedAttr[0]
            elif fkey = 'F2':
            return self.storedAttr[1]
            ...
            elif fkey = 'F12':
            return self.storedAttr[11]

            you would do something like this:

            # good way
            def get_attribute(f key):
            return self.attr[self.functionke ys[fkey]]


            [color=blue]
            > Also, how do I store/restore entries to the list?[/color]

            That question is awfully open-ended. Can you be more specific?


            [color=blue]
            > I have found the "copy" module, and it's copy method. I assume this
            > would work:
            >
            > for i in range(0, 10):
            > self.storedAttr .append(copy.co py(self.drawAtt r))
            >
            > However, the concept of "deep copy" confuses me. Do I want it, or
            > don't I want it? I repeat: the attributes object is a simple dictionary.[/color]

            That depends on what is inside your simple dictionary. For immutable
            objects like ints, floats and strings, copy is sufficient:
            [color=blue][color=green][color=darkred]
            >>> D1 = {1: 'hello', 2: 'there'}
            >>> D1[/color][/color][/color]
            {1: 'hello', 2: 'there'}[color=blue][color=green][color=darkred]
            >>> D2 = D1.copy()
            >>> D1[1] = 'go'
            >>> D1[/color][/color][/color]
            {1: 'go', 2: 'there'}[color=blue][color=green][color=darkred]
            >>> D2[/color][/color][/color]
            {1: 'hello', 2: 'there'}

            See what happens when the values are mutable objects:
            [color=blue][color=green][color=darkred]
            >>> DM1 = {1: [0,1], 2: [4, 5]}
            >>> DM2 = DM1.copy()
            >>> DM1[3] = [0,2]
            >>> DM1[/color][/color][/color]
            {1: [0, 1], 2: [4, 5], 3: [0, 2]}[color=blue][color=green][color=darkred]
            >>> DM2[/color][/color][/color]
            {1: [0, 1], 2: [4, 5]}

            So far so good. But now look:
            [color=blue][color=green][color=darkred]
            >>> DM1[1].append(999)
            >>> DM1[/color][/color][/color]
            {1: [0, 1, 999], 2: [4, 5], 3: [0, 2]}[color=blue][color=green][color=darkred]
            >>> DM2[/color][/color][/color]
            {1: [0, 1, 999], 2: [4, 5]}

            The difference is that although copy makes a copy of the top level of the
            dict, it DOESN'T make copies of the individual objects within the dict.

            This doesn't matter is the objects are immutable, but if they are lists or
            other dicts, you can get surprises like the above.


            --
            Steven.

            Comment

            Working...