How can I resolve circular references between two instances of a class in Python?
-
I have two instances of a class that competes in a simulation where they try to shoot at each other. The class contains a position variable and a target variable. The second instance's target variable references the first instance's position object, and vice-versa. I have a chicken-and-egg problem when creating the two instances, and when trying to bind the references after instantiating, they don't update properly. All other question related to this topic is discussed in the Data Science with Python Training. This is a simplified version of my code: class Thing(): def __init__(self, position, target): self.position = position self.target = target def move(self): self.position += 10 ## thing1 = Thing(position = 0, target = thing2.position) # Ideally this line would work... thing1 = Thing(position = 0, target = 0) thing2 = Thing(position = 100, target = thing1.position) print(thing1.target) thing1.target = thing2.position print(thing1.target) thing2.move() print(thing1.target) The output I get is 0,100,100, and the output I want is 0,100,110.