Assuming I have a system of three Classes.
The GameClass creates instances of both other classes upon initialization.
class FieldClass:
def __init__( self ):
return
def AnswerAQuestion( self ):
return 42
class PlayerClass:
def __init__( self ):
return
def DoMagicHere( self ):
# Access "AnswerAQuestion" located in the "FieldClass" instance in "GameClass"
pass
class GameClass:
def __init__( self ):
self.Field = FieldClass()
self.Player = PlayerClass()
What would be the best way of accessing AnswerAQuestion() located in FieldClass from within the instance of PlayerClass?
- Do I have to pass a reference to the
FieldClassinstance toPlayerClass? - Is there another, better way of solving this? Doing the above would make me have to include an additional variable in
PlayerClassto hold theFieldClassinstance. - Is there a completely different way of managing class relationships in Python?