tamuhey
tamuhey

Reputation: 3535

Iterable without side effect type annotation

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)
  1. Iterable is not suited because x is iterated more than once
  2. Sequence is not suited because the order of x doesn't matter

How to annotate x?

Upvotes: 1

Views: 113

Answers (2)

user2357112
user2357112

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

PMM
PMM

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

Related Questions