Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How can I resolve circular references between two instances of a class in Python?

How can I resolve circular references between two instances of a class in Python?

Scheduled Pinned Locked Moved C / C++ / MFC
questionannouncementpythoncomhelp
1 Posts 1 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Atonal Wilson
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups