Luc*_*uca 2 python typing type-hinting python-3.x
我有一个 python 函数,它返回具有以下结构的字典
{
(int, int): {string: {string: int, string: float}}
}
Run Code Online (Sandbox Code Playgroud)
我想知道如何使用类型提示来指定它。所以,这些位很清楚:
Dict[Tuple[int, int], Dict[str, Dict[str, # what comes here]]
Run Code Online (Sandbox Code Playgroud)
但是,内部字典具有两个键的int和值类型。float我不知道如何注释
您应该能够使用Union:
联合型;
Union[X, Y]表示 X 或 Y。
from typing import Union
Dict[Tuple[int, int], Dict[str, Dict[str, Union[int, float]]]
Run Code Online (Sandbox Code Playgroud)
话虽这么说,如果密钥始终相同,那么使用 atuple或 anamedtuple代替内部可能是更好的主意。dict
| 归档时间: |
|
| 查看次数: |
4826 次 |
| 最近记录: |