P
pang
Hello,
This is an idea about something I'd like to see implemented in
python.
I understand that's the purpose of PEPs, so I'll write it as a PEP,
but
send it here to receive your valuable feedback.
Abstract
This is a proposal to increase the richness of for loops, only to the
extent that it equals that of list and generator comprehensions. In
the
opinion of the proponent, this will make the language more uniform and
would reduce the excessive level of nesting that is required
sometimes,
without introducing special keywords, or breaking backwards
compatibility.
This PEP is independent of, but related to PEP 3142.
Rationale
The syntax of a for loop is restricted to the following:
Nesting for loops and conditionals is possible, of course:
However, for list and generator comprehensions, the syntax is more
concise:
Loops and comprehensions serve for similar purposes, and sometimes
what
was first written in one way is finally changed into the other. Thus
it
would be convenient to allow for more similar syntax in both of them.
This requires that the syntax of a for loop allows for nested for and
conditionals (and possibly while loops (see PEP 3142))
In general, whenever
As another example, it is sometimes the case that an iteration over
two
lists can be rewritten as a single iteration:it is somewhat more conceptual to think of the first iteration as a
single loop.
It is actually possible to come very close to the notation proposed
using the generator comprehensions introduced in python 2.5:but this notation is cumbersome.
This PEP responds to the philosophy that if a nesting level is unused,
it should be avoided. For example, in the code:
the second nesting level doesn't contain any statements and thus,
should this PEP be passed, it should be written:
This is an idea about something I'd like to see implemented in
python.
I understand that's the purpose of PEPs, so I'll write it as a PEP,
but
send it here to receive your valuable feedback.
Abstract
This is a proposal to increase the richness of for loops, only to the
extent that it equals that of list and generator comprehensions. In
the
opinion of the proponent, this will make the language more uniform and
would reduce the excessive level of nesting that is required
sometimes,
without introducing special keywords, or breaking backwards
compatibility.
This PEP is independent of, but related to PEP 3142.
Rationale
The syntax of a for loop is restricted to the following:
Nesting for loops and conditionals is possible, of course:
However, for list and generator comprehensions, the syntax is more
concise:
condition(x,y,...)]list=[func(x,y,..) for x in list1 for y in list2 ... if
Loops and comprehensions serve for similar purposes, and sometimes
what
was first written in one way is finally changed into the other. Thus
it
would be convenient to allow for more similar syntax in both of them.
This requires that the syntax of a for loop allows for nested for and
conditionals (and possibly while loops (see PEP 3142))
In general, whenever
should be a valid loop.[expression1 iterator] is a valid list comprehension, then
iterator:
statements
As another example, it is sometimes the case that an iteration over
two
lists can be rewritten as a single iteration:it is somewhat more conceptual to think of the first iteration as a
single loop.
It is actually possible to come very close to the notation proposed
using the generator comprehensions introduced in python 2.5:but this notation is cumbersome.
This PEP responds to the philosophy that if a nesting level is unused,
it should be avoided. For example, in the code:
the second nesting level doesn't contain any statements and thus,
should this PEP be passed, it should be written: