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 3 1620
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.
Peter
If you included some code, maybe we'd be able to help you. struct.unpack("2i", " "*8)
(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
"Uwe Mayer" <me*****@hadiko.de> wrote in message
news:bu**********@news.rz.uni-karlsruhe.de... 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 Thanks in advance Ciao Uwe This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Uwe Mayer |
last post by:
Hi,
sorry for the lack of source code.
Here again:
I use struct.unpack() to unpack data from a binary file and pass the
returned tuple as parameter to __init__ of a class that's supposed to...
|
by: Claire Reed |
last post by:
Dear All,
I am repeatedly encountering a problem whilst looping through XML
Nodes and I am unsure as to what is going on and how I can get around
it.
I load the following XML document into an...
|
by: Adam |
last post by:
Hi all,
I have an arraylist filled with 4 objects and defined as shown below.
ArrayList items = new ArrayList();
public Item GetItemAt(int index)
{
return (Item)items;
}
|
by: Haydnw |
last post by:
Hi,
I have the code below as code-behind for a page which displays two images.
My problem is with the second bit of code, commented as " 'Portfolio image
section". Basically, the SQL query gets...
|
by: tobias.sturn |
last post by:
Hi!
My prof told me always to make my members private or protected cause
its standard to write setter and getter methodes.. Is that in your
opinion correct? Cause I dont see any adventages to...
|
by: vincenzoelettronico |
last post by:
I am making the pages asp of one library on-linens with visual study 2003. I
have created the datagrid, the subroutine UpdateCommand. .now I want to
avoid to always insert the same element....
|
by: mikeficklonni |
last post by:
Hello,
I am very new to C# and .Net. I may be overlooking something very simple here, however, even after reading several forums I'm still stumped.
I can't get the HasChanges method to work. ...
|
by: raylopez99 |
last post by:
Hi,
Best practices question.
When receiving an object passed from another method, is it a good idea
to use a shallow copy with a temporary object received on the RHS
(right hand side of =),...
|
by: Jonathan Wood |
last post by:
Greetings,
I was wondering if anyone here has a good understaning of the Session
object. I know there are options like the Session.Abandon method and the
regenerateExpiredSessionId setting,...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |