Let's say I have a function that accepts a Garthok
, an Iterable[Garthok]
, an Iterable[Iterable[Garthok]]
, etc.
def narfle_the_garthoks(arg):
if isinstance(arg, Iterable):
for value in arg:
narfle_the_garthoks(arg)
else:
arg.narfle()
Is there any way to specify a type hint for arg that indicates that it accepts any level of Iterable
s of Garthok
s? I suspect not, but thought I'd check if I'm missing something.
As a workaround, I'm just specifying a few levels deep, and then ending with Iterable[Any]
.
Union[Garthok,
Iterable[Union[Garthok,
Iterable[Union[Garthok,
Iterable[Union[Garthok, Iterable[Any]]]]]]]]
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…