if iter(iterator) is iterator

Gregory Ewing greg.ewing at canterbury.ac.nz
Sun Nov 13 16:57:01 EST 2016


Antoon Pardon wrote:

> def bar(iterator):
>     if iter(iterator) is iterator:
>         ...

That's a way of finding out whether you can safely iterate
over something more than once. If the object is already an
iterator, applying iter() to it will return the same
object, and iterating over it will consume it.

You would only care if you're intending to iterate over
it more than once, and you're not sure what kind of object
you have.

-- 
Greg



More information about the Python-list mailing list