count items in generator

Alex Martelli aleax at mac.com
Sun May 14 18:27:56 EDT 2006


Cameron Laird <claird at lairds.us> wrote:

> In article <1hfarom.1lfetjc18leddeN%aleax at mac.com>,
> Alex Martelli <aleax at mac.com> wrote:
>                       .
>                       .
>                       .
> >My preference would be (with the original definition for
> >words_of_the_file) to code
> >
> >   numwords = sum(1 for w in words_of_the_file(thefilepath))
>                       .
>                       .
>                       .
> There are times when 
> 
>     numwords = len(list(words_of_the_file(thefilepath))
> 
> will be advantageous.

Can you please give some examples?  None comes readily to mind...


> For that matter, would it be an advantage for len() to operate
> on iterables?  It could be faster and thriftier on memory than
> either of the above, and my first impression is that it's 
> sufficiently natural not to offend those of suspicious of
> language bloat.

I'd be a bit worried about having len(x) change x's state into an
unusable one. Yes, it happens in other cases (if y in x:), but adding
more such problematic cases doesn't seem advisable to me anyway -- I'd
evaluate this proposal as a -0, even taking into account the potential
optimizations to be garnered by having some iterables expose __len__
(e.g., a genexp such as (f(x) fox x in foo), without an if-clause, might
be optimized to delegate __len__ to foo -- again, there may be semantic
alterations lurking that make this optimization a bit iffy).


Alex



More information about the Python-list mailing list