【发布时间】:2017-02-10 19:36:14
【问题描述】:
我有一个函数可以返回生成器 (types.GeneratorType) 或 list,具体取决于参数的值。例如:
def foo(switch: bool) -> list: # return type of list || types.GeneratorType
... # create some generator object named f1
if switch:
return list(f1)
else:
return f1
如何告诉它我可以选择返回列表或其他类型?或者,就此而言,一堆不同的类型呢?
我查看了documentation on a module typing,但还没有找到任何允许这样做的方法。我也无法通过谷歌搜索找到任何示例。
【问题讨论】:
标签: python python-3.x annotations type-hinting