I have two classes and I want both of them to hold instances of the other, which get passed in as a parameter and I want to use typing to have clean code. Without typing it works like this:
class PlayerBrain():
def setPlayer(self, player):
self.player = player
class Player():
def __init__(self, playerBrain):
self.playerBrain= playerBrain
playerBrain = PlayerBrain()
player = Player(playerBrain)
playerBrain.setPlayer(player)
With typing it won't work, because of the time of evaluating typing it doesn't know the other class yet:
class PlayerBrain():
def setPlayer(self, player: Player):
self.player = player
class Player():
def __init__(self, playerBrain):
self.playerBrain= playerBrain
playerBrain = PlayerBrain()
player = Player(playerBrain)
playerBrain.setPlayer(player)
Is there any option to solve this while using typing? Other programming languages solve this by forward declarations or just do it automatically, can Python do this too? Creating instances of the other class in the method body is not an option, they have to be passed in as a parameter.