Michael Seifert added the comment:

> I explored that notion of iterator length transparency years ago.  While I 
> don't remember all the details, I did record some notes at the top of 
> Lib/test/test_iterlen.py.

But isn't that the point of the length_hint? To provide an *estimate* for the 
length? So generally I would expect the length_hint to be accurate (at least 
accurate enough to avoid reallocations) for well-behaved iterators. And I think 
that's possible for "zip" and "map" (but also for several itertools: product, 
permutations, combinations, islice, accumulate, starmap, zip_longest). At least 
in theory.

Also it would allow to prohibit infinite iterators to be passed to 
"length_hint"-aware functions. For example `list(count())` could raise an 
exception when `count.length_hint` would return math.inf or sys.maxsize + 1.

The pull request was just because I was curious how it performed and wanted to 
share the code. Feel free to reject it. The performance improvement wasn't 
amazing in the micro-benchmarks.

----------
nosy: +MSeifert

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue26828>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to