[[1, 2, 3], 3, [2, 4], 5]
的类型为 list[list[int] | int]
。但是,如果嵌套列表的深度未定义,那么它会具有什么类型,例如[[[1, 2]], 2, [1, [3, [3]]]], 3, [2, [2]]]
?
最佳答案
您可以定义递归类型别名:
T: TypeAlias = list[Union[int, 'T']]
关于具有未定义嵌套列表深度的嵌套列表的Python注释类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73346562/