<div dir="ltr">First of all, that's amazing and exactly what I was looking for.<div><br></div><div>Second, sorry Nick, I guess we were talking past each other and I didn't understand what you were getting at.  From the collections.abc documentation, I imagined that subclasses are more restricted and therefore can do more than their superclasses.  However, as you were trying to tell me things that are "Iterators" (and thus also Iterable) can do *less* than things that are merely Iterable.  The former cannot be iterated over twice.   If I'm understanding this correctly, would it be nice if the documentation then made this promise (as I don't believe it does)?</div>

<div><br></div><div>Best,</div><div><br></div><div>Neil</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Sep 19, 2013 at 5:30 AM, Antoine Pitrou <span dir="ltr"><<a href="mailto:solipsis@pitrou.net" target="_blank">solipsis@pitrou.net</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Le Thu, 19 Sep 2013 04:59:35 -0400,<br>
Neil Girdhar <<a href="mailto:mistersheik@gmail.com">mistersheik@gmail.com</a>> a<br>
écrit :<br>
<div class="im">> Well, generators are iterable, but if you write a function like:<br>
><br>
> def f(s):<br>
>      for x in s:<br>
>              do_something(x)<br>
>      for x in s:<br>
>              do_something_else(x)<br>
><br>
> x should not be a generator.  I am proposing adding a function to<br>
> itertools like auto_reiterable that would take s and give you an<br>
> reiterable in the most efficient way possible.<br>
<br>
</div>Try the following:<br>
<br>
<br>
import collections<br>
import itertools<br>
<br>
<br>
class Reiterable:<br>
<br>
    def __init__(self, it):<br>
        self.need_cloning = isinstance(it, collections.Iterator)<br>
        assert self.need_cloning or isinstance(it, collections.Iterable)<br>
        self.master = it<br>
<br>
    def __iter__(self):<br>
        if self.need_cloning:<br>
            self.master, it = itertools.tee(self.master)<br>
            return it<br>
        else:<br>
            return iter(self.master)<br>
<br>
def gen():<br>
    yield from "ghi"<br>
<br>
for arg in ("abc", iter("def"), gen()):<br>
    it = Reiterable(arg)<br>
    print(list(it))<br>
    print(list(it))<br>
    print(list(it))<br>
<br>
<br>
I don't know if that would be useful as part of the stdlib.<br>
<br>
Regards<br>
<br>
Antoine.<br>
<div class="im"><br>
<br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
<br>
--<br>
<br>
</div>---<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/topic/python-ideas/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">python-ideas+unsubscribe@googlegroups.com</a>.<br>
For more options, visit <a href="https://groups.google.com/groups/opt_out" target="_blank">https://groups.google.com/groups/opt_out</a>.<br>
</blockquote></div><br></div>