typedef的Python等价物

2024-06-24 12:26:08 发布

您现在位置:Python中文网/ 问答频道 /正文

python以什么方式定义(非类)类型,如:

typedef Dict[Union[int, str], Set[str]] RecordType

Tags: 类型定义方式dictintunionsetstr
1条回答
网友
1楼 · 发布于 2024-06-24 12:26:08

这就行了吗

from typing import Dict, Union, Set

RecordType = Dict[Union[int, str], Set[str]]


def my_func(rec: RecordType):
    pass


my_func({1: {'2'}})
my_func({1: {2}})

此代码将在第二次调用my_func时从IDE生成警告,但在第一次调用时不会。正如@sahasrara62所指出的,这里有更多的信息https://docs.python.org/3/library/stdtypes.html#types-genericalias

相关问题 更多 >