This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Unification of list-comp and for syntax
Type: Stage:
Components: Interpreter Core Versions: Python 2.5
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: collinwinter, hwundram, jimjjewett
Priority: normal Keywords: patch

Created on 2006-05-21 15:06 by hwundram, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
python-for-if.diff hwundram, 2006-05-21 15:06 Patch to enable for <expr> in <expr> if <expr>: syntax.
Messages (5)
msg50319 - (view) Author: Heiko Wundram (hwundram) Date: 2006-05-21 15:06
The following patch adds the ability for:

for <expr> in <expr> if <expr>:
    <do something>

to the Python core. This unifies the syntax of
list/generator comprehensions and the for statement
somewhat, because both now accept conditions which
produce an immediate continue.

I've posted a PEP to python-dev, which details the
changes this patch makes (which are all
backwards-compatible).

The patch doesn't try to address more than the actual
code required to make this feature work yet (except for
changes to Modules/parsermodule.c and Doc/ref/ref7.tex,
which details the for statement). If there's consensus
on this feature, I'll gladly produce more documentation.
msg50320 - (view) Author: Jim Jewett (jimjjewett) Date: 2006-05-23 19:53
Logged In: YES 
user_id=764593

I'm not loving the interaction with conditional expressions.

for x in (1,2,3) if test else (3,2,1):

I suppose this techically isn't ambiguous because else is a 
keyword.

On the other hand, you could do it now using he if-else

for x in real_seq if test else ():

msg50321 - (view) Author: Jim Jewett (jimjjewett) Date: 2006-05-23 20:14
Logged In: YES 
user_id=764593

It seems I misread what the intent was -- I was thinking of 
the if as guarding the entire for loop, not just a single 
iteration.

Because of this confusion, I have to be -1.  

Is there a reason you can't just wrap your iterable 
sequence with another iterator?

for x in (candidate for candidate in fullseq if test):

msg50322 - (view) Author: Heiko Wundram (hwundram) Date: 2006-05-24 06:10
Logged In: YES 
user_id=791932

Sure, you can wrap the iterable, or you can even do:

if x in y:
    if not x:
        continue
    ...

or

if x in y:
    if x:
        ...

without using any form of "iterator magic". Read my PEP-xxx
on py-dev, and my explanation there of why I think this is a
"good thing"(TM), but I won't go explain it here again,
because generally people have told be to drop it.
msg50323 - (view) Author: Collin Winter (collinwinter) * (Python committer) Date: 2007-03-07 23:09
This was rejected by the BDFL in http://mail.python.org/pipermail/python-dev/2006-May/065090.html; closing.
History
Date User Action Args
2022-04-11 14:56:17adminsetgithub: 43391
2006-05-21 15:06:46hwundramcreate