GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • [Python-Dev] listcomps vs. for loops
  • [Python-Dev] listcomps vs. for loops
  • [ date ]
  • [ thread ]
  • [ subject ]
  • [ author ]

This view

  • Original page
  • New address

GeezerVison is showing mail.python.org — [Python-Dev] listcomps vs. for loops

[Python-Dev] listcomps vs. for loops

Walter Dörwald walter at livinglogic.de
Wed Oct 22 14:05:12 EDT 2003

  • Previous message: [Python-Dev] listcomps vs. for loops
  • Next message: [Python-Dev] listcomps vs. for loops
  • Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

Guido van Rossum wrote:

 > [...]
> >How about an until keyword in generator expressions:
>
>
> New keywords are not on the table for generator expressions.  You
> could do this with 'while' (which is just 'until not' -- note that
> your example uses that :-) 

You're right, using while would be better.

> but I'd be against making this part of the
> syntax more complex.  You can do that with itertools.takewhile or
> dropwhile anyway.

But

sum(len(line) for line in file if not line.startswith("#") while 
line.strip())

looks simple than

sum(itertools.takewhile(lambda l: l.strip(), len(line) for line in file 
if not line.startswith("#"))

>>def last(it):
>>	for value in it:
>>		pass
>>	return value
> 
> What if it is empty?

This should raise an exception.
(It does, but not the correct one! ;))

>>first(line for line in file if line.startswith("#"))
>>
>>if not last(file):
>>	# last line not terminated
> 
> 
> The comment is incorrect.

That should have been:
if not last(file).endswith("\n"):

Bye,
    Walter Dörwald




  • Previous message: [Python-Dev] listcomps vs. for loops
  • Next message: [Python-Dev] listcomps vs. for loops
  • Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

More information about the Python-Dev mailing list
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.