473,320 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

PEP-xxx: Unification of for statement and list-comp syntax

Hi all!

The following PEP tries to make the case for a slight unification of for
statement and list comprehension syntax.

Comments appreciated, including on the sample implementation.

===
PEP: xxx
Title: Unification of for-statement and list-comprehension syntax
Version: $Revision$
Last-Modified: $Date$
Author: Heiko Wundram <me@modelnine.org>
Status: Active
Type: Standards Track
Content-Type: text/plain
Created: 21-May-2006
Post-History: 21-May-2006 17:00 GMT+0200
Abstract

When list comprehensions were introduced, they added the ability
to add conditions which are tested before the expression which is
associated with the list comprehension is evaluated. This is
often used to create new lists which consist only of those items
of the original list which match the specified condition(s). For
example:

[node for node in tree if node.haschildren()]

will create a new list which only contains those items of the
original list (tree) whose items match the havechildren()
condition. Generator expressions work similarily.

With a standard for-loop, this corresponds to adding a continue
statement testing for the negated expression at the beginning of
the loop body.

As I've noticed that I find myself typing the latter quite often
in code I write, it would only be sensible to add the corresponding
syntax for the for statement:

for node in tree if node.haschildren():
<do something with node>

as syntactic sugar for:

for node in tree:
if not node.haschildren():
continue
<do something with node>

There are several other methods (including generator-expressions or
list-comprehensions, the itertools module, or the builtin filter
function) to achieve this same goal, but all of them make the code
longer and harder to understand and/or require more memory, because
of the generation of an intermediate list.
Implementation details

The implementation of this feature requires changes to the Python
grammar, to allow for a variable number of 'if'-expressions before
the colon of a 'for'-statement:

for_stmt: 'for' exprlist 'in' testlist_safe ('if' old_test)* ':'
suite ['else' ':' suite]

This change would replace testlist with testlist_safe as the
'in'-expression of a for statement, in line with the definition of
list comprehensions in the Python grammar.

Each of the 'if'-expressions is evaluated in turn (if present), until
one is found False, in which case the 'for'-statement restarts at the
next item from the generator of the 'in'-expression immediately
(the tests are thus short-circuting), or until all are found to be
True (or there are no tests), in which case the suite body is executed.
The behaviour of the 'else'-suite is unchanged.

The intermediate code that is generated is modelled after the
byte-code that is generated for list comprehensions:

def f():
for x in range(10) if x == 1:
print x

would generate:

2 0 SETUP_LOOP 42 (to 45)
3 LOAD_GLOBAL 0 (range)
6 LOAD_CONST 1 (10)
9 CALL_FUNCTION 1
12 GET_ITER
13 FOR_ITER 28 (to 44) 16 STORE_FAST 0 (x)
19 LOAD_FAST 0 (x)
22 LOAD_CONST 2 (1)
25 COMPARE_OP 2 (==)
28 JUMP_IF_FALSE 9 (to 40)
31 POP_TOP

3 32 LOAD_FAST 0 (x)
35 PRINT_ITEM
36 PRINT_NEWLINE
37 JUMP_ABSOLUTE 13 40 POP_TOP 41 JUMP_ABSOLUTE 13 44 POP_BLOCK
45 LOAD_CONST 0 (None)

48 RETURN_VALUE

where all tests are inserted immediately at the beginning of the
loop body, and jump to a new block if found to be false which pops
the comparision from the stack and jumps back to the beginning of
the loop to fetch the next item.

Implementation issues

The changes are backwards-compatible, as they don't change the
default behaviour of the 'for'-loop. Also, as the changes that
this PEP proposes don't change the byte-code structure of the
interpreter, old byte-code continues to run on Python with this
addition unchanged.
Implementation

A sample implementation (with updates to the grammar documentation
and a small test case) is available at:
http://sourceforge.net/tracker/index...70&atid=305470
Copyright

This document has been placed in the public domain.
===

--- Heiko.
May 21 '06 #1
6 2144
i wanted to suggest this myself. +1
-tomer

May 21 '06 #2
KW
On 2006-05-21, Heiko Wundram wrote:
Hi all!

The following PEP tries to make the case for a slight unification of for
statement and list comprehension syntax.


Sounds great!

--
Konrad
May 21 '06 #3
Heiko Wundram wrote:
The following PEP tries to make the case for a slight unification of for
statement and list comprehension syntax.


-1

Adds complexity to the language and saves you nothing but an indent
level. However, I encourage you to submit this PEP and get a (almost
certianly negative) pronouncement, so we have it on the record.
Carl Banks

May 21 '06 #4
Heiko Wundram wrote:
....
As I've noticed that I find myself typing the latter quite often
in code I write, it would only be sensible to add the corresponding
syntax for the for statement:

for node in tree if node.haschildren():
<do something with node>

as syntactic sugar for:

for node in tree:
if not node.haschildren():
continue
<do something with node>


Mhhh, your unsugared form remind me of darks hours with primitive BASICS in my
youth - the kind Dijsktra commented on. Why don't you write

for node in tree:
if node.haschildren():
<do something with node>

?
May 22 '06 #5
Am Montag 22 Mai 2006 11:27 schrieb Boris Borcic:
Mhhh, your unsugared form remind me of darks hours with primitive BASICS in
my youth - the kind Dijsktra commented on. Why don't you write

for node in tree:
if node.haschildren():
<do something with node>


As I've replied on python-dev, indentation is not always a good thing,
especially if the for-body is longer than a few lines.

The "if not: continue" form allows you to keep the indentation at one level,
so that it's pretty clear what is part of the loop body, and what is not. If
you add an extra indentation, your mind has to keep track of the indentation,
and will expect an "else:" somewhere, which in the use case I propose won't
happen. At least that's what my mind does, and is majorly confused, if the
else doesn't appear.

This certainly is personal taste, and AFAICT there are pretty few people who
feel like I do. But, basically, I find it easier to have one level of
indentation, and to test for the negated condition, than to put the loop body
in an enclosing if-statement, which will always add an extra level of
indentation. I put forth the proposal, because it allows you to save this
level of indentation, which makes the code more readable for me.

Anyway, the PEP has already been rejected on python-dev, and I'm currently
just rewriting it with the positive and negative things that have so far been
said, so basically, it'll just be there so that people can be pointed at it
when anybody else'll ask for it.

--- Heiko.
May 22 '06 #6
+1 It does seem like a natural unificiation of the language -- one less
exception to learn.

-- Russell
May 22 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: John Roth | last post by:
I'm adding a thread for comments on Gerrit Holl's pre-pep, which can be found here: http://tinyurl.com/2578q Frankly, I like the idea. It's about time that all of the file and directory stuff...
3
by: Christoph Becker-Freyseng | last post by:
Hello, recently there was a lot discussion about a new Path-class. So Gerrit Holl started to write a pre-PEP http://people.nl.linux.org/~gerrit/creaties/path/pep-xxxx.html We tried to...
6
by: Skip Montanaro | last post by:
I wrote PEP 304, "Controlling Generation of Bytecode Files": http://www.python.org/peps/pep-0304.html quite awhile ago. The first version appeared in January 2003 in response to questions...
8
by: Micah Elliott | last post by:
I also have this living as a wiki <http://tracos.org/codetag/wiki/Pep> if people would like to add comments there. I might try to capture there feedback from this group anyway. First try at a PEP...
77
by: Ben Finney | last post by:
Howdy all, PEP 354: Enumerations in Python has been accepted as a draft PEP. The current version can be viewed online: <URL:http://www.python.org/peps/pep-0354.html> Here is the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.