在子类化容器时添加基类类型提示

问题描述 投票:2回答:1

当我对容器进行子类化时,如何为我的定义的容器部分指定类型提示?我的意思是类似于(目前不支持)以下内容:

from typing import Dict

class MyDict(dict: Dict[str, int]):
    def __init__(...):
...

这样,当我尝试通过继承自基类的接口添加,删除等对象到我的dict时,可以对其进行类型检查。

python type-hinting
1个回答
2
投票

它的形式提到了here

from typing import Dict

class Test(Dict[str, int]):
    def __init__(self):
        self.update({'a': 5})

test = Test()
print(test) # prints {'a': 5}
© www.soinside.com 2019 - 2024. All rights reserved.