gpt4 book ai didi

Python 类型提示 : when to use MutableSequence vs List

转载 作者:行者123 更新时间:2023-12-01 23:58:42 26 4
gpt4 key购买 nike

我一直在阅读有关 typing.Sequencetyping.MutableSequence 的内容。两者都没有大量的“简单阅读”,一切都直接进入细节。

来自 this answerCan you specify variance in a Python type annotation?

Sequence is the read-only version of List

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


更多详情

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

通过阅读 Mutable sequences 部分,似乎 MutableSequence 可能是 List 的“父级”?

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

最佳答案

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

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

当你想表达“可变序列”时使用MutableSequence。当你想表达“列表”时使用List

关于Python 类型提示 : when to use MutableSequence vs List,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62239125/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com