<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On Jul 15, 2012, at 7:22 AM, Antoine Pitrou wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><span class="Apple-style-span" style="border-collapse: separate; font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; ">On Mon, 16 Jul 2012 00:08:41 +1000<br>Nick Coghlan <<a href="mailto:ncoghlan@gmail.com">ncoghlan@gmail.com</a>> wrote:<br><blockquote type="cite">Right, I agree on the value in being able to return something to say "this<br></blockquote><blockquote type="cite">cannot be converted to a concrete container".<br></blockquote><br>Who would be able to return that, apart from trivial cases like<br>itertools.cycle()?<br></span></blockquote></div><div><br></div><div>FWIW, here are the notes from the docstring in Lib/test/test_iterlen.py:</div><div><br></div><div><div>""" Test Iterator Length Transparency</div><div><br></div><div>Some functions or methods which accept general iterable arguments have</div><div>optional, more efficient code paths if they know how many items to expect.</div><div>For instance, map(func, iterable), will pre-allocate the exact amount of</div><div>space required whenever the iterable can report its length.</div><div><br></div><div>The desired invariant is: len(it)==len(list(it)).</div><div><br></div><div>A complication is that an iterable and iterator can be the same object. To</div><div>maintain the invariant, an iterator needs to dynamically update its length.</div><div>For instance, an iterable such as xrange(10) always reports its length as ten,</div><div>but it=iter(xrange(10)) starts at ten, and then goes to nine after it.next().</div><div>Having this capability means that map() can ignore the distinction between</div><div>map(func, iterable) and map(func, iter(iterable)).</div><div><br></div><div>When the iterable is immutable, the implementation can straight-forwardly</div><div>report the original length minus the cumulative number of calls to next().</div><div>This is the case for tuples, xrange objects, and itertools.repeat().</div><div><br></div><div>Some containers become temporarily immutable during iteration. This includes</div><div>dicts, sets, and collections.deque. Their implementation is equally simple</div><div>though they need to permanently set their length to zero whenever there is</div><div>an attempt to iterate after a length mutation.</div><div><br></div><div>The situation slightly more involved whenever an object allows length mutation</div><div>during iteration. Lists and sequence iterators are dynamically updatable.</div><div>So, if a list is extended during iteration, the iterator will continue through</div><div>the new items. If it shrinks to a point before the most recent iteration,</div><div>then no further items are available and the length is reported at zero.</div><div><br></div><div>Reversed objects can also be wrapped around mutable objects; however, any</div><div>appends after the current position are ignored. Any other approach leads</div><div>to confusion and possibly returning the same item more than once.</div><div><br></div><div>The iterators not listed above, such as enumerate and the other itertools,</div><div>are not length transparent because they have no way to distinguish between</div><div>iterables that report static length and iterators whose length changes with</div><div>each call (i.e. the difference between enumerate('abc') and</div><div>enumerate(iter('abc')).</div><div><br></div><div>"""</div></div><div><br></div><div><br></div><div>Raymond</div></body></html>