【发布时间】:2019-05-19 13:53:50
【问题描述】:
假设我有一个接受Garthok、Iterable[Garthok]、Iterable[Iterable[Garthok]] 等的函数。
def narfle_the_garthoks(arg):
if isinstance(arg, Iterable):
for value in arg:
narfle_the_garthoks(arg)
else:
arg.narfle()
有没有办法为 arg 指定一个类型提示,表明它接受Garthoks 的任何级别的Iterables?我怀疑不是,但我想我会检查我是否遗漏了什么。
作为一种解决方法,我只是指定了几个级别,然后以 Iterable[Any] 结尾。
Union[Garthok,
Iterable[Union[Garthok,
Iterable[Union[Garthok,
Iterable[Union[Garthok, Iterable[Any]]]]]]]]
【问题讨论】:
-
这能回答你的问题吗? Recursive type annotations
标签: python type-hinting python-typing