My problem is quite simple but I am unable to solve it. When I insert objects into a list, the elements of the list all change whenever I change one of them (they all point to the same object in the memory I think). I want to unlink them so the list would not be full of the exactly same objects with the same values. E.g. avoid linking or mutability. I think the problem is how I initialize the objects but I am not sure how to solve it. Here is my code.
from typing import List, Tuple
class State:
#think of State as some kind of coordinates
def __init__(self, z:float, angle:float):
self.z = z
self.angle = angle
class ListOfStates:
#this should be an object with a list containing DIFFERENT (unlinked) State objects
def __init__(self, list_of_states : List[State]):
self.list_of_states = list_of_states
class StateSettings:
#a bigger object to encapsulate previous objects
def __init__(self, state : State, list_of_states : ListOfStates):
self.state = state
self.list_of_states = list_of_states
some_number = 42
# my try #1
state_settings = StateSettings
#create a list of State objects to be used later
state_settings.list_of_states = [State for i in range(some_number)]
state_settings.state = State
for i in range(some_number):
state_settings.list_of_states[i].angle = i
And state_settings.list_of_states contains the same copy of the object 42 times, e.g.
print(state_settings.list_of_states[0].angle)
print(state_settings.list_of_states[1].angle)
print(state_settings.list_of_states[2].angle)
prints
41
41
41
I also tried different ways to initialize, but with no luck.
# my try #2
state_settings = StateSettings(
state = State(
z = 0,
angle = 0),
list_of_states = [State for i in range(some_number)]
)
for i in range(some_number):
state_settings.list_of_states[i].angle = i
or
# my try 3
from copy import deepcopy
state_settings = StateSettings
state_settings.list_of_states = [deepcopy(State) for i in range(some_number)]
state_settings.state = deepcopy(State)
for i in range(some_number):
state_settings.list_of_states[i].angle = i
My question, as far as I know, is not solved by answers such as Changing a single object within an array of objects changes all, even in a different array or List of Objects changes when the object that was input in the append() function changes.