字典的类型提示给出TypeError:'type'对象不可下标[重复]

问题描述 投票:-2回答:2
memo: dict[int, int] = {0: 0, 1: 1} # *our base cases*

返回以下错误:

TypeError: 'type' object is not subscriptable
python python-3.x python-typing
2个回答
2
投票

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

from typing import Dict

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

在您的情况下,您正在使用类型为dicttype

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

0
投票

您可以执行以下操作:

from typing import Dict
mydict = Dict[int, int]

mydict = {0: 0, 1: 1} 

结果:

>>>mydict
{0: 0, 1: 1}

如果这是您想要的。

© www.soinside.com 2019 - 2024. All rights reserved.