Aug-06-2024, 01:36 PM
(This post was last modified: Aug-06-2024, 02:41 PM by deanhystad.)
My list is overwritten, but I don't see why. The objects are new instances and the list assignment is doing using copy, but the first object's list values are lost after I created the second one.
My code:
Please, help.
My code:
class Hex: @classmethod def __init__(self, cdHex, myList): self.cdHex = cdHex self.myList = myList.copy() def getmyList(self): return(self.myList) d_hexes={} d_hexes2={} hexAct='A' myList=[2] d_hexes.update({hexAct:Hex(hexAct,myList)}) d_hexes2.update({hexAct:myList}) print('First') print(d_hexes[hexAct].getmyList()) print(d_hexes['A'].getmyList()) print(d_hexes2['A']) hexAct='B' myList=[3] d_hexes.update({hexAct:Hex(hexAct,myList)}) d_hexes2.update({hexAct:myList}) print('Second') print(d_hexes[hexAct].getmyList()) print(d_hexes['A'].getmyList()) print(d_hexes2['A']) exit()Console results:
Output:First
[2]
[2]
[2]
Second
[3]
[3]
[2]
The second [3] should be [2].Please, help.
deanhystad write Aug-06-2024, 02:41 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.