<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>On Sep 30, 2015, at 10:47, Chris Barker <<a href="mailto:chris.barker@noaa.gov">chris.barker@noaa.gov</a>> wrote:</div><div><br></div><blockquote type="cite"><div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Wed, Sep 30, 2015 at 10:33 AM, Serhiy Storchaka <span dir="ltr"><<a href="mailto:storchaka@gmail.com" target="_blank">storchaka@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On 30.09.15 20:18, Neil Girdhar wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Ah good point. Well, in the case of a sequence argument, an enumerate<br>
object could be both a sequence and an iterator.<br>
</blockquote>
<br></span>
It can't be.<br>
<br>
For sequence:<br>
<br>
>>> x = 'abcd'<br>
>>> list(zip(x, x))<br>
[('a', 'a'), ('b', 'b'), ('c', 'c'), ('d', 'd')]<br>
<br>
For iterator:<br>
<br>
>>> x = iter('abcd')<br>
>>> list(zip(x, x))<br>
[('a', 'b'), ('c', 'd')]</blockquote><div><br></div><div>well, that's because zip is using the same iterator it two places. would that ever be the case with enumerate?</div></div></div></div></div></blockquote><div><br></div><div>The point is that _nothing_ can be an iterator and a sequence at the same time. (And therefore, an enumerate object can't be both at the same time.)</div><div><br></div><div>The zip function is just a handy way of demonstrating the problem; it's not the actual problem. You could also demonstrate it by, e.g., calling len(x), next(x), list(x): If x is an iterator, next(x) will use up the 'a' so list will only give you ['b', 'c', 'd'], even though len gave you 4.</div><div><br></div><div>Conceptually: iterators are inherently one-shot iterables; sequences are inherently reusable iterables. While there's no explicit rule that __iter__ can't return self for a sequence, there's no reasonable way to make a sequence that does so. Which means no sequence can be an iterator.</div></body></html>