如何继承 Python 中的列表项?

How do inherit list items in Python?

我正在 Python 学习面向对象编程,并正在制作一款冒险游戏。

每个房间都是一个继承自场景的对象实例class。在每个房间中,我都有一个可以在该房间中使用的命令列表。该程序根据此列表检查用户输入以查看命令是否匹配(然后继续执行适当的功能:去另一个房间,拿起钥匙,诸如此类)。

我希望场景 class 包含任何房间的库存命令列表(帮助、库存等)。但是当引擎检查每个特定房间中的命令时,它会覆盖 superclass 中的命令列表。我如何更改此代码,以便 class Castle(Scene) 中命令中的项目也包含 class Scene(object) 中命令中的项目?

抱歉,如果这对你们来说有点基础。这里有类似的问题,但我无法在我的代码中真正理解它们。我是 OOP 新手。

class Scene(object):
    commands = [
        'help',
        'inventory'
        ]

    def action(self, command):
        if command == 'inventory':
            print "You are carrying the following items:"
            # function to display items will go here


class Castle(Scene):
    def enter(self):
        print "You are in a castle"

    commands = [
        'get key',
        'east'
        ] 

    def action(self, command):
        if command == 'get key':
            print "You pick up the key"
            return 'castle'
        elif command == 'east':
            print "You go east"
            return 'village'
        else:
            pass
        return(0)

您可以使用属性:

>>> class A(object):
...     @property
...     def x(self):
...         return [1]
...
>>>
>>> class B(A):
...     @property
...     def x(self):
...         return super(B, self).x + [2]
...
>>> b = B()
>>> b.x
[1, 2]
>>>