>      def consume2(iterator, n):  # the approved proposal (see #7764)
>          if n is None:
>              collections.deque(iterator, maxlen=0)
>          else:
>              next(islice(iterator, n, n), None)

FWIW, the deque() approach becomes even faster in Py2.7 and Py3.1
which has a high-speed path for the case where maxlen is zero.
Here's a snippet from Modules/_collectionsmodule.c:

/* Run an iterator to exhaustion.  Shortcut for
   the extend/extendleft methods when maxlen == 0. */
static PyObject*
consume_iterator(PyObject *it)
{
        PyObject *item;

        while ((item = PyIter_Next(it)) != NULL) {
                Py_DECREF(item);
        }
        Py_DECREF(it);
        if (PyErr_Occurred())
                return NULL;
        Py_RETURN_NONE;
}


This code consumes an iterator to exhaustion.
It is short, sweet, and hard to beat.


Raymond
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to