<div dir="ltr">Why not do it the way Antoine suggested, but instead of <div><br></div><div><span style="font-family:arial,sans-serif;font-size:13px">self.need_cloning = isinstance(it, collections.Iterator)</span><br></div>


<div><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div><span style="font-family:arial,sans-serif;font-size:13px">have</span></div><div><span style="font-family:arial,sans-serif;font-size:13px"><br>


</span></div><div><span style="font-family:arial,sans-serif;font-size:13px">self.need_cloning = isinstance(it, collections.Reiterable)</span><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div>


<span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div><span style="font-family:arial,sans-serif;font-size:13px">Then, mark the appropriate classes as subclasses of collections.Reiterable where collections.Sequence < collections.Reiterable < collections.Iterable?</span></div>

<div><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div><span style="font-family:arial,sans-serif;font-size:13px">Best,</span></div><div><span style="font-family:arial,sans-serif;font-size:13px"><br>

Neil</span></div><div class="gmail_extra">
<br><br><div class="gmail_quote">On Thu, Sep 19, 2013 at 5:40 PM, Terry Reedy <span dir="ltr"><<a href="mailto:tjreedy@udel.edu" target="_blank">tjreedy@udel.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


<div>On 9/19/2013 8:18 AM, Steven D'Aprano wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On Thu, Sep 19, 2013 at 07:12:26PM +1000, Nick Coghlan wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
is there any obvious case where "iterable but<br>
not an iterator" gives the wrong answer?<br>
</blockquote>
<br>
I'm not sure if it counts as "obvious", but one can write an iterator<br>
that is re-iterable. A trivial example:<br>
<br>
class Reiter:<br>
     def __init__(self):<br>
         self.i = 0<br>
     def __next__(self):<br>
         i = self.i<br>
         if i < 10:<br>
             self.i += 1<br>
             return i<br>
         self.i = 0<br>
</blockquote>
<br></div>
This, I agree, is bad.<div><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
         raise StopIteration<br>
     def __iter__(self):<br>
         return self<br>
<br>
<br>
I know that according to the iterator protocol, such a re-iterator<br>
counts as "broken":<br>
<br>
[quote]<br>
The intention of the protocol is that once an iterator’s next() method<br>
raises StopIteration, it will continue to do so on subsequent calls.<br>
</blockquote>
<br></div>
I would add 'unless and until iter() or another reset method is called. Once one pokes at a iterator with another mutation method, all bets are off. I would consider Reiter less broken or not at all if the reset in __next__ were removed, since then it would continue to raise until explicity reset with __iter__<div>


<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Implementations that do not obey this property are deemed broken. (This<br>
constraint was added in Python 2.3; in Python 2.2, various iterators are<br>
broken according to this rule.)<br>
<br>
<a href="http://docs.python.org/2/library/stdtypes.html#iterator-types" target="_blank">http://docs.python.org/2/<u></u>library/stdtypes.html#<u></u>iterator-types</a><br>
<br>
but clearly there is a use-case for re-iterable "things", such as dict<br>
views, which can be re-iterated over. We just don't call them iterators.<br>
So maybe there should be a way to distinguish between "oops this<br>
iterator is broken" and "yes, this object can be iterated over<br>
repeatedly, it's all good".<br>
<br>
At the moment, dict views aren't directly iterable (you can't call<br>
next() on them). But in principle they could have been designed as<br>
re-iterable iterators.<br>
<br>
Another example might be iterators with a reset or restart method, or<br>
similar. E.g. file objects and seek(0). File objects are officially<br>
"broken" iterators, since you can seek back to the beginning of the<br>
file. I don't think that's a bad thing.<br>
<br>
But nor am I sure that it requires a special Reiterable class so we can<br>
test for it.<br>
<br>
<br>
</blockquote>
<br>
<br>
-- <br></div>
Terry Jan Reedy<div><div><br>
<br>
<br>
______________________________<u></u>_________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/<u></u>mailman/listinfo/python-ideas</a><br>
<br>
-- <br>
<br>
--- You received this message because you are subscribed to a topic in the Google Groups "python-ideas" group.<br>
To unsubscribe from this topic, visit <a href="https://groups.google.com/d/topic/python-ideas/OumiLGDwRWA/unsubscribe" target="_blank">https://groups.google.com/d/<u></u>topic/python-ideas/<u></u>OumiLGDwRWA/unsubscribe</a>.<br>



To unsubscribe from this group and all its topics, send an email to <a href="mailto:python-ideas%2Bunsubscribe@googlegroups.com" target="_blank">python-ideas+unsubscribe@<u></u>googlegroups.com</a>.<br>
For more options, visit <a href="https://groups.google.com/groups/opt_out" target="_blank">https://groups.google.com/<u></u>groups/opt_out</a>.<br>
</div></div></blockquote></div><br></div></div>