[Python-Dev] if-syntax for regular for-loops

Vitor Bosshard algorias at yahoo.com
Fri Oct 3 18:03:20 CEST 2008



----- Mensaje original ----
> De: Leif Walsh <leif.walsh at gmail.com>
> Para: Andreas Nilsson <adde at trialcode.com>
> CC: python-dev at python.org
> Enviado: viernes, 3 de octubre, 2008 10:29:33
> Asunto: Re: [Python-Dev] if-syntax for regular for-loops
> 
> On Fri, Oct 3, 2008 at 6:10 AM, Andreas Nilsson wrote:
> > With that out of the way, on to todays subject:
> > I use list comprehensions and generator expressions a lot and lately I've
> > found myself writing a lot of code like this:
> >
> > for i in items if i.some_field == some_value: i.do_something()
> >
> > Naturally it won't work but it seems like a pretty straight-forward
> > extension to allow compressing simple loops to fit on one line. The
> > alternative, in my eyes, suggests there's something more happening than a
> > simple include-test which makes it harder to comprehend.
> >
> > for i in items:
> >        if i.some_field == some_value: i.do_something()
> >
> > One possibility of course is to use a generator-expression but that makes it
> > look like there are two for loops and it feels like a waste setting up a
> > generator just for filtering.
> >
> > for i in (i for i in items if some_field == some_value):
> >        i.do_something()
> >
> > Stupid idea? Am I missing some obviously better way of achieving the same
> > result?
> 
> It's been discussed already.  Essentially, all that saves is a newline
> or two, which, as I think has been generally accepted, tends to hurt
> readability.

The exact same argument could be used for list comprehensions themselves. They exist anyway, creating inconsistency in the language (being almost identical to for loops regarding syntax)

Vitor


      ____________________________________________________________________________________
Premios MTV 2008¡En exclusiva! Fotos, nominados, videos, y mucho más! Mira aquí http://mtvla.yahoo.com/


More information about the Python-Dev mailing list