count items in generator

Cameron Laird claird at lairds.us
Mon May 15 11:02:10 EDT 2006


In article <1hfc2sb.8hxfcv13c5v6nN%aleax at mac.com>,
Alex Martelli <aleax at mac.com> wrote:
>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...
			.
			.
			.
Maybe in an alternative universe where Python style emphasizes
functional expressions.  This thread--or at least the follow-ups
to my rather frivolous observation--illustrate how distinct is
Python's direction.

If we could neglect memory impact, and procedural side-effects,
then, sure, I'd argue for my len(list(...)) formulation, on the
expressive grounds that it doesn't require the two "magic tokens"
'1' and 'w'.  Does category theory have a term for formulas of
the sort that introduce a free variable only to ignore (discard,
...) it?  There certainly are times when that's apt ...



More information about the Python-list mailing list