[Python-ideas] List-comprehension-like extensions in normal for loops

Josiah Carlson josiah.carlson at gmail.com
Mon Mar 3 01:35:02 CET 2008


Or even...

for record in (r for r in db record.name == 'pierre'):
  ...

Hello generator expressions (available since Python 2.4).  But I'm
with Eyal, personally.

 - Josiah

On Sat, Mar 1, 2008 at 2:57 PM, Eyal Lotem <eyal.lotem at gmail.com> wrote:
> How about just nesting the for's/if's?
>
>
>  for record in db:
>   if record.name == 'pierre':
>     ...
>
>  Isn't that the one obvious way to do it?
>
>
>
>  On Sun, Mar 2, 2008 at 12:24 AM, Adam Atlas <adam at atlas.st> wrote:
>  > How about letting ordinary for loops contain multiple "for"s and
>  >  optionally "if"s like in list comprehensions and generator
>  >  expressions? For example, on a site I was just looking at, it had the
>  >  line:
>  >      for record in [ r for r in db if r.name == 'pierre' ]:
>  >  which could instead be:
>  >      for record in db if record.name == 'pierre':
>  >
>  >  And of course there could be much more complex ones too. I know you
>  >  could just do "for x in <some generator expression>" and get the same
>  >  effect and roughly the same speed, but I think this looks a lot nicer,
>  >  and it would make sense to have this sort of consistency across the
>  >  multiple contexts in which "for" can be used.
>  >  _______________________________________________
>  >  Python-ideas mailing list
>  >  Python-ideas at python.org
>  >  http://mail.python.org/mailman/listinfo/python-ideas
>  >
>  _______________________________________________
>  Python-ideas mailing list
>  Python-ideas at python.org
>  http://mail.python.org/mailman/listinfo/python-ideas
>



More information about the Python-ideas mailing list