Reputation: 3535
I'm looking for Iterable
which is able to be iterated without side effect.
For example, I want to annotate the below args x
:
def foo(x: NoSideEffectIterable[int]):
for i in range(10):
for xx in x: # x is iterated 10 times here
print(i*xx)
Iterable
is not suited because x
is iterated more than onceSequence
is not suited because the order of x
doesn't matterHow to annotate x
?
Upvotes: 1
Views: 113
Reputation: 281594
There's no annotation for that. It's not a concept the type system can express. Iterable
is your best bet. If you want to get a bit more restrictive, you can use Collection
, but while Iterable
expresses less requirements than you want, Collection
expresses more.
Upvotes: 1
Reputation: 376
Try with Iterator[int]
. Iterators in Python are iterables that get consumed after you iterate them over once. Hope it helps
Upvotes: 0