是否可以使用 Python 类型提示将字典的键和值的类型指定为对?
例如 :
int,则值应该是 astrstr,value 应该是 anint如果我写:
Dict[Union[int, str], Union[int, str]]
Run Code Online (Sandbox Code Playgroud)
它允许str -> str和int -> int,这是不允许的。
与 :
Union[Dict[int, str], Dict[str, int]]
Run Code Online (Sandbox Code Playgroud)
字典可以是a或Dict[int, str],Dict[str, int]但不能同时是两者......
我也研究过TypedDict,但它需要明确给出所有密钥。