Python 类型提示:何时使用 MutableSequence 与 List

Python type hinting: when to use MutableSequence vs List

我一直在阅读有关 typing.Sequencetyping.MutableSequence 的内容。两者都没有太多 "light reading",一切都直接进入细节。

来自 to

Sequence is the read-only version of List

所以这让我想知道,MutableSequence 和普通的 List 有什么区别?


更多详情

我能找到的最佳来源是 Python 数据模型的 The standard type hierarchy 部分。

通过阅读 Mutable sequences 部分,MutableSequence 似乎是 List 的 "parent"?

换句话说,它们可以互换使用,只是 MutableSequence 限制少了一点?

MutableSequence 表示任意可变序列。例如array.array的实例满足MutableSequence.

List 特别是列表。如果对象不是列表,则不满足 List.

想表达"mutable sequence"时用MutableSequence。当你想表达 "list".

时使用 List