when an iterable object is exhausted or not

Ramchandra Apte maniandram01 at gmail.com
Sun Aug 5 00:56:04 EDT 2012


An important use of range repeating.
one_to_10 = range(1,10)
one_to_5 = range(1,5)
for x in one_to_5:
 for x in one_to_10:pass
if range wasn't repeatable, range would have to be called 5 times compared
with 1!
On 5 August 2012 07:43, Steven D'Aprano <
steve+comp.lang.python at pearwood.info> wrote:
> On 2012年8月04日 21:20:36 +0200, Franck Ditter wrote:
>> > Two similar iterable objects but with a different behavior :
> [...]
> > IMHO, this should not happen in Py3k. What is the rationale of this (bad
> > ?) design, which forces the programmer to memorize which one is
> > exhaustable and which one is not ?...
>> What makes you say that they are "similar" iterable objects? Except that
> they are both iterable, they are very different. You might as well say
> that lists and dicts are "similar iterable objects".
>> filter objects are iterators, and so obey the intentionally simple
> iterator protocol. range objects are iterable but not iterators, and do
> not obey the iterator protocol.
>> py> it = filter(lambda x: x, set('abc'))
> py> iter(it) is it
> True
> py> x = range(1, 15, 2)
> py> iter(x) is x
> False
>>> filter relies on its input, which it consumes as it does. Since the input
> may not be restartable, filter cannot be restartable either. For
> simplicity, filter does not try to be "clever" and guess when the input
> argument is restartable. Instead, it simply and consistently behaves the
> same for any iterable input.
>> range is a specialist iterable data structure that does not consume
> anything. It computes its output lazily, but that is the only similarity
> with iterators. There's no need to limit range objects to the simple
> iterator protocol, since they don't consume their input -- a single
> object can easily compute its output as often as you want. range objects
> are indexable and sliceable:
>> py> x = range(1, 15, 2)
> py> x[4]
> 9
> py> x[2:4]
> range(5, 9, 2)
>>> Why artificially make range objects unrestartable just to satisfy
> compatibility with iterators?
>> The caller already has to remember that range and filter take different
> arguments, do different things, and return different objects. Why is it
> hard to remember that range is restartable and filter is not?
>>> --
> Steven
> --
> http://mail.python.org/mailman/listinfo/python-list
>-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120805/4b92451c/attachment.html>


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /