always the same object

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

    always the same object

    Hi,

    I am using struct.unpack() quite often. unpack() returns a tuple, however a
    tuple with always the same id(), which is AFAIK the memory location of the
    structure.

    I found myself working with above tuple, initialising other data structures,
    etc... and when I tried to change one list element, suddenly all list
    elements change => so they all contain the identical object

    Now what are the rules when Python re-uses an old object (as with
    struct.unpack() ) and when does it create new objects?
    And what ist the proposed solution for dealing with this situation, i.e. how
    to I create a true copy of what struct.unpack() returns me?

    Thanks in advance
    Ciao
    Uwe
  • Peter Otten

    #2
    Re: always the same object

    Uwe Mayer wrote:
    [color=blue]
    > Hi,
    >
    > I am using struct.unpack() quite often. unpack() returns a tuple, however
    > a tuple with always the same id(), which is AFAIK the memory location of
    > the structure.
    >
    > I found myself working with above tuple, initialising other data
    > structures, etc... and when I tried to change one list element, suddenly
    > all list elements change => so they all contain the identical object
    >
    > Now what are the rules when Python re-uses an old object (as with
    > struct.unpack() ) and when does it create new objects?
    > And what ist the proposed solution for dealing with this situation, i.e.
    > how to I create a true copy of what struct.unpack() returns me?[/color]

    Most likely your problems have nothing to do with struct.unpack() , as it
    returns a tuple containing - as far as I know - only immutable objects. I
    suppose you are using the same list multiple times later in your script,
    and making a shallow copy

    otherList = list(someList)

    should suffice to remedy the observed "change once, see anywhere"
    experience. However, this is hard to tell without any sourcecode. So in the
    future, please take the time to compose a minimal script reproducing the
    observed behaviour along with a short description of what the script is
    supposed to do.

    Peter


    Comment

    • Jeff Epler

      #3
      Re: always the same object

      If you included some code, maybe we'd be able to help you.
      [color=blue][color=green][color=darkred]
      >>> struct.unpack(" 2i", " "*8)[/color][/color][/color]
      (538976288, 538976288)

      struct.unpack returned a tuple. The tuple happens to be immutable, and
      all the things it contains are immutable. So there's no way to change
      ever change the value of this object. As far as I know, this would be
      true for any struct format you care to choose.

      (You could change the "value" of an immutable object if it contains some
      mutable objects, like this:
      a = ([],)
      b = ([],)
      assert a == b
      a[0].append(0)
      assert a != b
      a and b go from equal to unequal, even though a and b are both
      immutable)

      Jeff

      Comment

      • John Roth

        #4
        Re: always the same object


        "Uwe Mayer" <merkosh@hadiko .de> wrote in message
        news:bukbuo$rqp $1@news.rz.uni-karlsruhe.de...[color=blue]
        > Hi,
        >
        > I am using struct.unpack() quite often. unpack() returns a tuple, however[/color]
        a[color=blue]
        > tuple with always the same id(), which is AFAIK the memory location of the
        > structure.
        >
        > I found myself working with above tuple, initialising other data[/color]
        structures,[color=blue]
        > etc... and when I tried to change one list element, suddenly all list
        > elements change => so they all contain the identical object[/color]

        The biggest problem people have with lists is attempting to
        reuse one rather than starting with a new one.

        For example, this ***WILL NOT*** work:

        class Fubar:
        aList = [None, None, None]

        def addToList(self, something):
        aList[:3] = something

        on the other hand, this will work:

        class Good:
        def __init__(self):
        self.aList = [None, None, None]

        def addToList(self, something):
        aList[:3] = something

        [color=blue]
        > Now what are the rules when Python re-uses an old object (as with
        > struct.unpack() ) and when does it create new objects?[/color]

        As far as lists are concerned, list literals ([...]) always return a
        new list.
        [color=blue]
        > And what ist the proposed solution for dealing with this situation, i.e.[/color]
        how[color=blue]
        > do I create a true copy of what struct.unpack() returns me?[/color]

        See above.

        John Roth[color=blue]
        >
        > Thanks in advance
        > Ciao
        > Uwe[/color]


        Comment

        Working...