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