0

I am pretty new to the whole OOP and class prt of python. So I was reaing this book where I got this snippet..

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

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

    def next_scene(self, scene_name):
        return Map.scenes.get(scene_name)

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

In the last line why did we have to use return self.next_scene(self.start_scene) instead of return next_scene(start_scene). Also can anyone explain the code and the output a bit when input is

a_map = Map('central_corridor')
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user3475283
  • 25
  • 1
  • 8
  • Because if you did *not* use `self` you'd get a name error. – Martijn Pieters Mar 23 '15 at 17:12
  • start_scene is a member of each of your class **instances**. hence `self.start_scene` is defined, but `start_scene` isn't. – Félix Cantournet Mar 23 '15 at 17:14
  • in the last line `start_scene` is not in the scope of that method... in the `__init__` method there is a local var `start_scene` which is passed in as an argument. That is then assigned as an instance attribute `self.start_scene` which is what you need to use in the `opening_scene` method, as the code above does – Anentropic Mar 23 '15 at 17:15

0 Answers0