I've encountered a problem where, during the initialization of my main class, I create an instance of another class and attach it to an attribute:
# DOES NOT WORK (game instance not initialised yet)
class Bar:
def __init__(self):
self.radius = game.object_one.width / 2
class Game:
def __init__(self):
self.object_one = Foo()
self.object_two = Bar()
game = Game()
I was able to overcome this by simply passing the instance as a parameter:
class Bar:
def __init__(self, obj):
self.radius = obj.width / 2
class Game:
def __init__(self):
self.object_one = Foo()
self.object_two = Bar(self.object_one)
game = Game()
This seems quite a band-aid solution for a seemingly simple problem. Is there a nicer way of doing this? Or is this entire problem a result of bad code? How can it be avoided in the future?