python - Is there a way to make a reference to a class object before I instantiate it? -


i trying play around python making simple text game. have room class:

class room():     def __init__(self, monster, exits, loot):         self.room_guard = monster         self.exits = exits         self.guard_is_alive = true         self.loot = loot 

when creating rooms getting error because call them before created so:

room_2 = room(spider, {"west": room_3, "east": room_4, "south": room_1}, 2) room_1 = room(trogdor, {"north": room_2}, 2) 

room 2 can't have "south": room_1 because hasn't been instantiated. there way around this?

two options: indirection, assignment after creation.

rather referring rooms directly use dict maps names of rooms rooms:

rooms = {} rooms['room_2'] = room(spider, {"west": 'room_3', "east": 'room_4', "south": 'room_1'}, 2) rooms['room_1'] = room(trogdor, {"north": 'room_2'}, 2) 

or assign exits after room objects have been created:

room_2 = room(spider, {}, 2) room_1 = room(trogdor, {}, 2)  room_2.exits = {"west": room_3, "east": room_4, "south": room_1} room_1.exits = {"north": room_2} 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -