实现代表 "a list with a title" 的 class 的 pythonic 方式是什么?

What would be the pythonic way to implement a class that represents "a list with a title"?

我试图在 python 中更好地理解组合与继承,因此我制作了一个希望简单的示例。

假设我想实现一个 class TitledList ,它的行为就像一个标准列表,但有一个额外的属性 title 用来保存一个包含名称的字符串的名单。 __repr____str__ 方法可能必须重新实现以合并标题,但除此之外我想保留 list 的所有本机功能。初始化程序将接受一个可选的 keyword-only 参数 title.

我可以看到两种创建此 class 的方法。我可以走合成路线,并定义一个 class 和两个属性,.contents(常规列表)和 .title。这大概看起来类似于

class TitledList:
    def __init__(self, contents=[], *, title=None):
        self.contents = list(contents)
        self.title = title

但我不确定我将如何抄袭 list 的所有方法,这样我就不必经常到处引用 my_titled_list.contents 或重新实现所有列出我使用的方法。

另一种选择是做整个互联网上每个人 says not to do 的事情,并继承 list class。我想在那种情况下我会像这样做初始化程序吗?

class TitledList(list):
    def __init__(self, iterable=[], *, title=None):
        super().__init__(iterable)
        self.title = title

这对我来说似乎更直接。但可以肯定的是,整个互联网上的每个人都说不要扩展 list,原因是

这里的优缺点是什么?有没有一种简单的方法可以使组合解决方案按照我直观的方式工作?继承解决方案有很多我不明白的缺点吗?我应该考虑第三种选择吗 (UserList?)

有几种方法可以做到。

  1. Subclass collections.UserList 而不是 list。这基本上是围绕 list 设计的可扩展包装器。

  2. Subclass collections.abc.MutableSequence 并实现所有需要的抽象方法。如果您想定义一个全新的序列,如 class.

  3. ,这可能很有用
  4. 亚classlist。这在有限的情况下实际上可能没问题,比如您的 TitledList 示例,您只是在其中添加一个新属性。但是如果你想覆盖 list 的现有方法,那么这可能不是一个好主意。

  5. 创建一个具有列表属性的新对象。这简单易懂,但可能会带来不便。这完全取决于您的用例。

参考:Trey Hunner: The problem with inheriting from dict and list in Python