always the same object

Peter Otten __peter__ at
Wed Jan 21 00:46:02 CET 2004

Uwe Mayer wrote:

> 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.


More information about the Python-list mailing list