[Python-ideas] Make len() usable on a generator

Thomas Chaumeny t.chaumeny at gmail.com
Sun Oct 5 01:28:33 CEST 2014


On Sat, Oct 4, 2014 at 3:27 PM, Steven D'Aprano <steve at pearwood.info> wrote:

>
> I don't understand this reasoning. Why do you think that they are
> unnecessary?


Sorry, that was a bad formulation. Obvisouly you need to increment a
counter at some point when iterating over the generator.

Now, if you have a builtin function len (or other), you can define it to
run a fast iterating loop using a C integer to maintain the number of
elements during the loop. If you use sum(1 for a in  ...), I suppose there
is some overhead due do the fact that you have to deal with Python objects
when doing the additions.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20141005/d2dbb550/attachment.html>


More information about the Python-ideas mailing list