首页 > 解决方案 > dict 的类型提示给出 TypeError: 'type' object is not subscriptable

问题描述

memo: dict[int, int] = {0: 0, 1: 1} # *our base cases*

返回以下错误:

TypeError: 'type' object is not subscriptable

标签: pythonpython-3.xpython-typing

解决方案


我想你应该使用Dict,例如:

from typing import Dict

memo: Dict[int, int] = {0: 0, 1: 1}

在您的情况下,您使用dict的是 which 类型type

>>> type(dict)
<class 'type'>
>>> type(Dict)
<class 'typing._GenericAlias'>

推荐阅读