Andreas Nilsson | 3 Oct 12:08

if-syntax for regular for-loops

Hi.
First post so here it goes.
My name is Adde, and I'm a Swedish software developer. I've been  
programming for about 23 years now since starting with Basic on the  
C64. I've been through most well known and a couple of lesser known  
languages in search of the perfect one. At the moment I'm writing a  
custom ctypes interface to the Firebird database (need access to  
advanced features, portability to Windows and I definitely don't enjoy  
setting up GCC on Windows).
I've programmed a lot of C/C++ in my days so I thought I'd at least  
join the list and see if anything piques my interest enough to dive in.

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.

(Continue reading)

David Gowers | 3 Oct 12:35

Re: if-syntax for regular for-loops

Hi Andreas,

On Fri, Oct 3, 2008 at 7:40 PM, Andreas Nilsson <adde <at> trialcode.com> wrote:
> Hi.
> First post so here it goes.
> My name is Adde, and I'm a Swedish software developer. I've been programming
> for about 23 years now since starting with Basic on the C64. I've been
> through most well known and a couple of lesser known languages in search of
> the perfect one. At the moment I'm writing a custom ctypes interface to the
> Firebird database (need access to advanced features, portability to Windows
> and I definitely don't enjoy setting up GCC on Windows).
> I've programmed a lot of C/C++ in my days so I thought I'd at least join the
> list and see if anything piques my interest enough to dive in.
>
> 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
(Continue reading)

Leif Walsh | 3 Oct 16:29

Re: if-syntax for regular for-loops

On Fri, Oct 3, 2008 at 6:10 AM, Andreas Nilsson <adde <at> trialcode.com> 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.

Here's the full discussion:
(Continue reading)

Vitor Bosshard | 3 Oct 18:02
Favicon
Gravatar

Re: if-syntax for regular for-loops


----- 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()
> >
(Continue reading)

Greg Ewing | 4 Oct 01:34

Re: if-syntax for regular for-loops

Vitor Bosshard wrote:
> 
>>On Fri, Oct 3, 2008 at 6:10 AM, Andreas Nilsson wrote:
>>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.

No, an LC saves more than newlines -- it saves the code
to set up and append to a list. This is a substantial
improvement when this code would otherwise swamp the
essentials of what's being done.

This doesn't apply to a plain for-loop that's not
building a list.

--

-- 
Greg
_______________________________________________
Python-Dev mailing list
Python-Dev <at> python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/python-python-dev%40m.gmane.org

Nick Coghlan | 4 Oct 04:26

Re: if-syntax for regular for-loops

Greg Ewing wrote:
> Vitor Bosshard wrote:
>> The exact same argument could be used for list comprehensions themselves.
> No, an LC saves more than newlines -- it saves the code
> to set up and append to a list. This is a substantial
> improvement when this code would otherwise swamp the
> essentials of what's being done.
> 
> This doesn't apply to a plain for-loop that's not
> building a list.

Not only do LCs make it obvious to the reader that "all this loop does
is build a list", but the speed increases from doing the iteration in
native code rather than pure Python are also non-trivial - every pass
through the main eval loop that can be safely avoided leads to a fairly
substantial time saving.

Generally speaking, syntactic sugar (or new builtins) need to take a
construct in idiomatic Python that is fairly obvious to an experienced
Python user and make it obvious to even new users, or else take an idiom
that is easy to get wrong when writing (or miss when reading) and make
it trivial to use correctly.

Providing significant performance improvements (usually in the form of
reduced memory usage or increased speed) also counts heavily in favour
of new constructs.

I strongly suggest browsing through past PEPs (both accepted and
rejected ones) before proposing syntax changes, but here are some
examples of syntactic sugar proposals that were accepted.
(Continue reading)

Steven D'Aprano | 4 Oct 05:08

Re: if-syntax for regular for-loops

On Sat, 4 Oct 2008 12:26:30 pm Nick Coghlan wrote:

> (Tangent: the above two try/except examples are perfectly legal Py3k
> code. Do we really need the "pass" statement anymore?)

I can't imagine why you would think we don't need the pass statement. I 
often use it:

* For subclassing exceptions:

class MyTypeError(TypeError):
    pass

* As a placeholder for code I haven't written yet.
* As a no-op used in, e.g. the timeit module.

And probably a few other places as well.

--

-- 
Steven
_______________________________________________
Python-Dev mailing list
Python-Dev <at> python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/python-python-dev%40m.gmane.org

Eric Smith | 4 Oct 11:30
Favicon

Re: if-syntax for regular for-loops

Steven D'Aprano wrote:
> On Sat, 4 Oct 2008 12:26:30 pm Nick Coghlan wrote:
> 
>> (Tangent: the above two try/except examples are perfectly legal Py3k
>> code. Do we really need the "pass" statement anymore?)
> 
> I can't imagine why you would think we don't need the pass statement. I 
> often use it:
> 
> * For subclassing exceptions:
> 
> class MyTypeError(TypeError):
>     pass
> 
> * As a placeholder for code I haven't written yet.
> * As a no-op used in, e.g. the timeit module.
> 
> And probably a few other places as well.

Nick is literally (pardon the pun) saying that '...' could take the 
place of 'pass' in 3.0. His original examples are valid syntax, as 
written with the ellipses. For example:

$ ./python
Python 3.0rc1+ (py3k:66789, Oct  4 2008, 05:26:45)
[GCC 4.1.2 20070626 (Red Hat 4.1.2-13)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> try:
...   ...
... except:
(Continue reading)

Nick Coghlan | 5 Oct 11:53

Re: if-syntax for regular for-loops

Eric Smith wrote:
> I think it's a little too cute, and 'pass' is preferable.

Agreed - it was just a little surreal to put the "..." in as my usual
pseudo-code "stuff happens here" marker, only to realise what I had
written was still executable Py3k code.

Cheers,
Nick.

--

-- 
Nick Coghlan   |   ncoghlan <at> gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
            http://www.boredomandlaziness.org
_______________________________________________
Python-Dev mailing list
Python-Dev <at> python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/python-python-dev%40m.gmane.org


Gmane