Python 类型提示: 类型化。映射与类型化

我正在做一个 python3项目,在这个项目中我们一直使用 typing模块类型提示。

似乎我们使用 typing.Dicttyping.Mapping几乎可以互换。

有没有理由选择其中一个而不是另一个?

39431 次浏览

Managed to answer this myself.

typing.Dict should be used to indicate a literal dict type with support for element type hinting i.e. Dict[bytes, str]

typing.Mapping is an object which defines the __getitem__,__len__,__iter__ magic methods

typing.MutableMapping is an object which defines same as Mapping but with __setitem__,__delitem__ magic methods as well.

typing.Mapping et al. are based on the abc types in this table