Re: [python-iter] Resolution at Python 9
- From: Ka-Ping Yee <ping@...>
>Is it for use as
> 7. A two-argument built-in function (provisionally, "iter()" will
> do this when given two arguments) will produce an iterator that
> repeatedly calls the callable object given as the first argument
> until it returns the sentinel value given as the second argument.
for x in iter(f,sentinel):
> I think the closest way to do it in Python is to use threads.This is already implemented: look at the Queue module:
> A thread-aware queue which is an iterator at one end and a file-like
> sink at the other, implemented once for all uses. Called Chan in
> Concurrent Haskell. Should be a nice abstraction. Doesn't require
> more magic than is already there.
> Well, so there are three approaches, and the third does fit Python!
To push an item into the Queue, you use q.put(x); to pull the next
item from it, use x = q.get(). If you want a terminator, all you need
to do is agree on a sentinel value. If you want an iterator, you can
use iter(q, sentinel).
The disadvantage is that it's way slow compared to stackframe-based
solutions, and that's why it's not commonly used as an alternative for
push/pull interfaces except where threads are already on the mind of
--Guido van Rossum (home page: http://www.python.org/~guido/)