comprehensions was Re: Switch statements again
dave at pythonapocrypha.com
Mon Jan 20 07:16:36 CET 2003
On Sun, 19 Jan 2003, Beni Cherniavsky wrote:
> > At the time, lexical nesting didn't yet exist in Python either.The case
> > for "magically local" bindings has gotten stronger since lexical nesting was
> > added, but now we've got a backwards compatibility burden too.
> There is one central use for leaving the binding alive in for loops: when
> you can exit it prematurely and want to record where you did. This is one
> thing missing from list comprehensions - a way to break in the middle.
I dunno - IMO a list comprehension is for building a list from another
list, so the only legitimate reasons for breaking out prematurely would be
some sort of optimization on processing ordered lists or maybe some sort
of "select first n occurences of f(x)" or something - cases not common
enough to warrant special list comp behavior since such needs can be
handled much better by plain for loops.
> Especially needed when iterating over infinite interators
But why use a list comprehension for that?
More information about the Python-list