python - Python中不可变对象(immutable对象)的类型是什么(针对mypy)

标签 python python-3.x mypy

我总是在我的 Python 程序中使用 mypy

不可变对象(immutable对象)的类型(来自 typing)是什么,可以用作字典键的对象?

回到上下文,我想写一个从字典继承的类,我有以下代码

class SliceableDict(dict):
    def __getitem__(self, attr:Any) -> Any:
        return super().__getitem__(attr)

在那种情况下,类型提示是毫无用处的,不是吗?

谢谢

最佳答案

dict 的键是 hashable (参见 the first sentence of the docs on dict ),可哈希类型为 typing.Hashable , collections.abc.Hashable 的别名.

关于python - Python中不可变对象(immutable对象)的类型是什么(针对mypy),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52564624/

相关文章:

python - 在 python 中使用 Timer 时出错?

python - 如何注释采用可变长度元组的函数? (可变元组类型注解)

python - 输出 : functions vs. 分配给变量的函数

python - 生成具有不同大小的透明层的合成图像

python-3.x - googleapiclient.errors.HttpError 与 google Drive v3 api 上传错误

python-3.x - Mypy + flake8 : Is there any way to surpress warning of `F821 undefined name`

python - 如何为另一个包打包类型 stub ?

python - 为什么一个方法可以引用一个 undefined variable ?

python - Scikit 中数据集的初始可视化 - head() 命令

python - 将生成的轮文件复制到目的地