Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Many answers come close to the point but miss it.</p> <p>An <strong><code>Iterator</code></strong> is an object that is consumed by iterating over it. There is no way around it. Example of iterator objects are those returned by calls to <a href="http://docs.python.org/2/library/functions.html#iter" rel="nofollow"><code>iter()</code></a>, or those returned by the functions in the <a href="http://docs.python.org/2/library/itertools.html" rel="nofollow"><code>itertools</code> module</a>.</p> <p>The proper way to check whether an object is an iterator is to call <code>isinstance(obj, Iterator)</code>. This basically checks whether the object implements the <code>next()</code> method (<code>__next__()</code> in Python 3) but you don't need to care about this.</p> <p>So, remember, an iterator is always consumed. For example:</p> <pre><code># suppose you have a list my_list = [10, 20, 30] # and build an iterator on the list my_iterator = iter(my_list) # iterate the first time over the object for x in my_iterator: print x # then again for x in my_iterator: print x </code></pre> <p>This will print the content of the list just <strong>once</strong>.</p> <p>Then there are <code>Iterable</code> objects. When you call <code>iter()</code> on an iterable it will return an iterator. Commenting in this page I made myself an error, so I will clarify here. Iterable objects are not required to return a <em>new iterator</em> on every call. Many iterators themselves are iterables (i.e. you can call <code>iter()</code> on them) and they will return the object itself.</p> <p>A simple example for this are list iterators. <code>iter(my_list)</code> and <code>iter(iter(my_list))</code> are the same object, and this is basically what @glglgl answer is checking for.</p> <p>The <a href="http://docs.python.org/2/library/stdtypes.html#iterator-types" rel="nofollow">iterator protocol</a> requires iterator objects to return themselves as their own iterator (and thus be iterable). This is not required for the iteration mechanics to work, but you wouldn't be able to loop over the iterator object.</p> <p>All of this said, what you should do is check whether you're given an Iterator, and if that's the case, make a copy of the result of the iteration (with <code>list()</code>). Your <code>isconsumable(obj)</code> is (as someone already said) <code>isinstance(obj, Iterator)</code>.</p> <p>Note that this also works for <code>xrange()</code>. <code>xrange(10)</code> returns an <code>xrange</code> object. Every time you iter over the xrange objects it returns a new iterator starting from the start, so you're fine and don't need to make a copy.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload