我的函数foo
接受参数things
,该参数在内部被转换为列表。
def foo(things):
things = list(things)
# more code
list
构造函数接受任何可迭代的。
但是,用things
注释typing.Iterable
并不能为用户提供线索,即可迭代项必须是有限的,而不是itertools.count()
之类的东西。
在这种情况下使用的正确类型提示是什么?
我不知道在Python中实现此目标的任何可能方法,因为您不能在类型提示中提供此类约束。
但是,Collection
可能在您的上下文中作为解决方法很有用:
Collection
用于大小可迭代的容器类的ABC。