【问题标题】:How does jumping from class to class in python work?在 python 中从一个班级跳到另一个班级是如何工作的?
【发布时间】:2013-01-25 18:47:40
【问题描述】:

这个问题基于 Zed Shaw 的 Learn Python the Hard Way,练习 43。在这个练习中,Zed 正在从一个班级跳到另一个班级,我的问题是它是如何工作的? 有两件事让我感到困惑:

  1. 事实上,返回“死亡”,返回类“死亡”。 return 之前的 'death' 中的 'd' 是小写的,但实际的类名以大写的“D”开头。为什么这仍然有效?

  2. return 'death' 是用引号引起来的。为什么这行得通,但是:

    def function():
        Variable = "This is a variable."
        return 'variable'
    
    print function()
    

只返回字符串“变量”。

一些例子是:

        print "putting him down, then jump through the Weapon Armory door."
        return 'laser_weapon_armory'

这个选择导致了一个风格化为“LaserWeaponArmory”的类。

     print "bridge where you must place it in the right spot."
     return 'the_bridge'

这会返回一个类型为“TheBridge”的类。

完整练习链接:

http://learnpythonthehardway.org/book/ex43.html

谢谢。

【问题讨论】:

  • 不返回任何类;只有字符串。

标签: python class object inheritance methods


【解决方案1】:

Python 没有进行任何此类转换。返回的字符串使用 Map 类手动映射到对象,该类在“您应该看到的内容”部分之前进行了描述。

这是Engine中使用的实际代码:

next_scene_name = current_scene.enter()
current_scene = self.scene_map.next_scene(next_scene_name)

next_scene_name 是一个字符串,下一行检索在Map 中为该字符串定义的类。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-05
    • 1970-01-01
    • 2018-07-24
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2021-11-18
    相关资源
    最近更新 更多