当前位置: 首页 > 知识库问答 >
问题:

如何在Python中附加类的实例,然后提取实例的属性?

魏楷
2023-03-14

我正在制作一个文本冒险游戏。

我创建了一个类,并创建了该类的几个不同实例。这个类有很多属性。我试图将类的每个实例附加到一个列表中。然后我希望能够从列表中的一个实例中提取一个属性。

我被告知要实施:

不要让每个房间都是[description,north,east,south,west]的列表,而是创建一个房间类。该类应该有一个构造函数,该构造函数接受(description,north,east,south,west)并设置描述字段和所有方向。让程序使用新类

这是我到目前为止的代码:

room_list = []

class Room():
    def __init__(self, describe, nw, n, ne, e, se, s, sw, w):
        self.description = describe
        self.northwest = nw
        self.north = n
        self.northeast = ne
        self.east = e
        self.southeast = se
        self.south = s
        self.southwest = sw
        self.west = w

kitchen = Room("You are in the Kitchen. Look at the scrumptious roast chicken and kidney pudding! \nThere are doors leading to North, East, and West.", None, 4, None, 2, None, None, None, 0)

room_list.append(kitchen)

east_cooridor = Room("You apparated into the East Corridor. \nYou can apparate to the Northwest or the Southwest.", 8, None, None, None, None, None, 2, None)

room_list.append(east_cooridor)

great_hall = Room("You are in the Great Hall. What is that great smell? \nThere appears to be doors leading to the north and the south.", None, 7, None, None, None, 1, None, None)

room_list.append(great_hall)

west_cooridor = Room("You apparated into the West Corridor. \nYou can apparate to the Northeast or the Southeast.", None, None, 6, None, 0, None, None, None)

room_list.append(west_cooridor)

owlery = Room("You are in the Owlery. WHOOs got mail? There is a glass door overlooking the Forbidden Forest. \nThere are doors in every direction.", None, 9, None, 8, None, 4, None, 6)

room_list.append(owlery)

forbidden_forest = Room("Yikes! You are in the Forbidden Forest! Is that a dead unicorn? ...Or is it Aragog? \nGet back into the Owlery, quick! \nOf course...if you wanted to explore the forest more, you could certainly try!", None, None, None, None, None, 7, None, None)

room_list.append(forbidden_forest)

current_room = 4
while done == False:
    print(room_list[current_room][0])

最后一行代码出错并显示:内置。TypeError:“房间”对象不支持索引

取而代之的是,我想让它从猫头鹰房里抽出“你在猫头鹰房里。谁收到邮件了?有一扇玻璃门可以俯瞰禁林。四面八方都有门”这句话。

到目前为止,我所尝试的都没有成功。如果有任何可能的建议,我将不胜感激。非常感谢。

共有1个答案

嵇星海
2023-03-14

听起来你不太明白属性是如何工作的。类中的属性通常不能作为数组访问。例如,如果要打印房间的描述,应显式调用属性:

current_room = 4
while not done:  # In python, "not done" is preferred over "done == False"
    print(room_list[current_room].description)
 类似资料:
  • 问题内容: 好的,这是真实的情况:我正在编写一个应用程序,并且有一个表示某种类型文件的类(在我的情况下,这是照片,但是细节与问题无关)。Photograph类的每个实例对于照片的文件名应该是唯一的。 问题是,当用户告诉我的应用程序加载文件时,我需要能够确定文件何时已加载,并使用该文件名的现有实例,而不是在同一文件名上创建重复的实例。 对我来说,使用记忆化似乎是一种好情况,并且有很多这样的例子,但是

  • 问题内容: 如果我从中创建函数的基类是派生该实例的类的基类,那么如何查找在Python中创建对象实例的类的名称? 我想也许检查模块可能在这里帮助了我,但似乎没有给我我想要的东西。除了解析__class__成员之外,我不确定如何获取此信息。 问题答案: 你是否尝试过该类的属性?即会给你班级的名字,我想这就是你想要的。 此方法仅适用于新式类。你的代码可能使用一些旧式类。这两种方法均适用:

  • 主要内容:类变量(类属性),实例变量(实例属性),局部变量无论是类属性还是类方法,都无法像普通变量或者函数那样,在类的外部直接使用它们。我们可以将类看做一个独立的空间,则类属性其实就是在类体中定义的变量,类方法是在类体中定义的函数。 前面章节提到过,在类体中,根据变量定义的位置不同,以及定义的方式不同,类属性又可细分为以下 3 种类型: 类体中、所有函数之外:此范围定义的变量,称为类属性或类变量; 类体中,所有函数内部:以“self.变量名”的方式定义的

  • 问题内容: 我是python的新手,了解到类属性就像C ++中的静态数据成员一样。但是,尝试以下代码后,我感到困惑: f2.a是否也等于5? 如果将a定义为列表而不是整数,则预期行为: 我研究了 Python:类和实例属性之间的区别,但是它不能回答我的问题。 谁能解释为什么会有所不同?谢谢 问题答案: 在第二个示例中,您没有做相同的事情。在第一个示例中,您要分配一个新值: 在第二个示例中,您只是在

  • rank ▲ ✰ vote url 56 390 59 554 url 如何获取实例的类名 如何才能获取一个实例对象的类名? 我想或许the inspect module可以实现,但是好像还没有发现什么能帮我的.或许可以分析__class__成员,但是我不知道怎么用. 你试没试类里的__name__属性?比如x.__class__.__name__或许可以得到你想要的 >>> import ite

  • 上面的代码准备好了两个类,请看表演 接着来