有人能解释一下这个简短的python方法吗

有人能解释一下这个简短的python方法吗,python,class,methods,Python,Class,Methods,我正在学习Python教程,但不理解这个类。 有人能给我解释一下下一个场景方法是如何工作的吗。 为什么要切换到下一个场景 class Map(object): scenes = { 'central_corridor': CentralCorridor(), 'laser_weapon_armory': LaserWeaponArmory(), 'the_bridge': TheBridge(), 'escape_pod

我正在学习Python教程,但不理解这个类。

有人能给我解释一下下一个场景方法是如何工作的吗。 为什么要切换到下一个场景

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)

它根据传递给它的关键字,从
场景
字典实例化场景对象,并将其返回给您

为什么要切换到下一个场景

它切换到下一个场景的原因是,在基类中,每个场景扩展指定了在通过
enter()
函数完成运行后序列中的下一个场景:

class Engine(object):

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

    def play(self):
        current_scene = self.scene_map.opening_scene()

        while True:
            print "\n--------"
            next_scene_name = current_scene.enter()  # returns next scene key
            current_scene = self.scene_map.next_scene(next_scene_name)  # initiates next scene and sets it as `current_scene`
例如,
CentralCorridor
场景结束时,根据输入的动作返回下一场景的键:

def enter(self):
    print "The Gothons of Planet Percal #25 have invaded your ship and destroyed"
    ...
    print "flowing around his hate filled body.  He's blocking the door to the"
    print "Armory and about to pull a weapon to blast you."

    action = raw_input("> ")

    if action == "shoot!":
        print "Quick on the draw you yank out your blaster and fire it at the Gothon."
        ...
        print "you are dead.  Then he eats you."
        return 'death'  # next scene `Death`

    elif action == "dodge!":
        print "Like a world class boxer you dodge, weave, slip and slide right"
        ...
        print "your head and eats you."
        return 'death'  # next scene `Death`

    elif action == "tell a joke":
        print "Lucky for you they made you learn Gothon insults in the academy."
        ...
        return 'laser_weapon_armory'  # next scene `LaserWeaponArmory`

    else:
        print "DOES NOT COMPUTE!"
        return 'central_corridor'  # next scene `CentralCorridor`
整个序列以死亡场景退出程序的
enter()
函数结束:

def enter(self):
    print Death.quips[randint(0, len(self.quips)-1)]
    exit(1)

返回Map.scenes.get(场景名称)

Map.scenes
是在类中定义的字典。对其调用
.get()
将获取给定键的值。本例中给出的键是
scene\u name
。然后该函数返回场景的一个实例

它类似于:

return scenes[scene_name]

除了在钥匙不在的情况下引发钥匙错误外,
None
将被返回。

在方法
next\u scene
中,您将传递一个
scene\u name
变量

test_map = Map('central_corridor')
test_map.next_scene('the_bridge')
当传递它时,它会检查类中的字典
场景
,并尝试从中获取所选场景


使用
get
方法,因此如果使用无效的场景名称调用该方法(例如
test\u map.next\u场景(\u计算机
),则不会引发
KeyError
异常。在这种情况下,它只会返回
None

下一个场景的
名称是误导性的。它不会根据某种内在顺序给出“下一个”场景,而是返回一个您选择的场景,可能是您想要选择的下一个场景。

它不会切换到下一个场景。它会切换到你在传递给函数的参数中指定的场景。我不太确定你在问什么。。。你到底面临什么问题?谢谢,我没有注意到!这让事情变得更清楚了。