Ex43 Where is get() defined?

Hello all,

I am trying to understand the code of ex43.

I have arrived to this part of the code:

class Map(object):
    scenes = {
        'central_corridor': CentralCorridor(),
        'laser_weapon_armory': LaserWeaponArmory(),
        'the_bridge': TheBridge(),
        'escape_pod': EscapePod(),
        'death': Death(),
        'finished': Finished(),
    }

    def __init__(self, start_scene):
        self.start_scene = start_scene

    def next_scene(self, scene_name):
       **val = Map.scenes.get(scene_name)**  # Where is this get() method defined and can somebody explain what Map.scene.get(scene_name) does? 
        return val

    def opening_scene(self):
        return self.next_scene(self.start_scene)

Question: Where is this get() method defined and can somebody explain what Map.scene.get(scene_name) does? It feels like the object would have to refer to itself/message itself once more, and I do not understand why this is necessary. I have also inserted the comment next to the code.

Hi all,

I solved my problem. I got all wrapped up in the Classes and Objects Theme that I forgot about dictionaries.

val = Map.scenes.get(scene_name) simply returns the value for the specific inserted key, which at the beginning of the program, the key is ‘central_corridor’ and the value is the class CentralCorridor().

1 Like

Yep, that’s the answer. Now check this out:

a_map = Map('central_corridor')
print(a_map.__dict__)

Try looking at the .__dict__ on any object to see what’s inside it.

1 Like

Hello @zedshaw,

Got it! Yes, a very useful method for understanding code.

1 Like