Suppose I'm writing a function taking in an iterable, and my function wants to be agnostic as to whether that iterable is actually an iterator yet or not.
(This is a common situation, right? I think basically all the itertools functions are written this way. Take in an iterable, return an iterator.)
If I call, for instance, itertools.tee(•, 2) on an object, and it happens to not be an iterator yet, that presumably means it would be cheaper just to call iter on it twice to get my two independent iterators. Are itertools functions smart enough to know this, and if not, what's the best way to avoid unnecessary costs in this way?
Observe:
So you do not need to worry whether the argument to your function is an iterable or already an iterator. You can call method
__iter__on something that is already an iterator and it just returnsselfin that case. This is not an expensive call and would be cheaper than anything you could possibly do to test to see if it is an iterator, such as whether it has a__next__method (and then having to call__iter__on it anyway if it doesn't).Update
We now see that there is a bit difference in passing to your function an iterable vs passing an iterator (depending on how the iterator is written, of course) since calling
itertwice on the former will give you two distinct iterators while callingitertwice on the latter will not.itertools.tee, as an example, is expecting an iterable. If you pass it an iterator that implements__iter__that returns 'selfit will clearly work sincetee` does not need two independent iterators for it to do its magic.But if you are writing an iterator that is passed an iterable that is implemented by internally using two or more iterators on the passed iterator, what you really want to be testing for is whether what is being passed is something that support multiple, concurrent, independent iterations regardless of whether it is an iterator or just a plain iterator:
Prints:
The writer of the original, passed iterator must write it in such a way that it supports multiple, concurrent, independent iterations.