0

I'm having a problem with two objects I created. I have a while loop getting some data. I save this data in an object and on each loop I want to see if my data has changed. If its true I save the data on a log. The problem is that when I refresh my actVar my prevVar is getting this changed too. This is my code:

I tried to use the copy() function with no success

uvcomsSup = UVCEComsCtrl_SupportData()
uvcomsAntSup = UVCEComsCtrl_SupportData()
while 1:
 try: 
 uvcomsSup = getUVComsSupport(OrigAddress, UVComsDestAddress) 
 print(uvcomsSup)
 print("________________________")
 print(uvcomsAntSup) 
 print(uvcomsSup is uvcomsAntSup)
 if (uvcomsSup != uvcomsAntSup):
 uvcomsAntSup = copy.copy(uvcomsSup) 
 logFile.addSupportData(str(uvcomsSup)) 
 sleep(1)
 except Exception as err:
 print("Error inesperado:", sys.exc_info()[0])
 print(err)

When I print this two objects (I have an eq method and str method) I see the same values. I just enter in the if condition the first time.

Any help?

Thank you

marc_s
760k186 gold badges1.4k silver badges1.5k bronze badges
asked Apr 10, 2019 at 12:47
1
  • Try to use deepcopy instead of copy Commented Apr 10, 2019 at 12:54

2 Answers 2

1

I cannot run your code so I cannot see the whole picture.

However I can see you are returning a shallow copy of ucomsSup, which retains references to the original object thus modifying it.

Try using a deep copy instead.

uvcomsSup = UVCEComsCtrl_SupportData()
uvcomsAntSup = UVCEComsCtrl_SupportData()
while 1:
 try: 
 uvcomsSup = getUVComsSupport(OrigAddress, UVComsDestAddress) 
 print(uvcomsSup)
 print("________________________")
 print(uvcomsAntSup) 
 print(uvcomsSup is uvcomsAntSup)
 if (uvcomsSup != uvcomsAntSup):
 uvcomsAntSup = copy.deepcopy(uvcomsSup) ## Create deep copy 
 logFile.addSupportData(str(uvcomsSup)) 
 sleep(1)
 except Exception as err:
 print("Error inesperado:", sys.exc_info()[0])
 print(err)
answered Apr 10, 2019 at 12:51
Sign up to request clarification or add additional context in comments.

Comments

0

The Python docs say:

copy(x) Return a shallow copy of x.

A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.

Try using copy.deepcopy(x) instead.

uvcomsAntSup = copy.deepcopy(uvcomsSup) 

copy.deepcopy(x) Return a deep copy of x.

A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.

Read more about this: https://docs.python.org/3.7/library/copy.html

answered Apr 10, 2019 at 12:55

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.