always the same object

U

Uwe Mayer

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
 
P

Peter Otten

Uwe said:
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?

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
 
J

Jeff Epler

If you included some code, maybe we'd be able to help you.
(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
 
J

John Roth

Uwe Mayer said:
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

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

Now what are the rules when Python re-uses an old object (as with
struct.unpack() ) and when does it create new objects?

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

See above.

John Roth
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top