[Python-Dev] For/while/if statements/comprehension/generator expressions unification

Alexander Myodov maa_public at sinn.ru
Tue Sep 20 08:26:59 CEST 2005

Hello Josiah,

JC> Alexander,
JC> The essence of what you have proposed has been proposed (multiple times) before,
JC> and I seem to remember it was shot down.

To increase my understanding of Python-way, can you (or someone else)
explain the reasons why such proposals were rejected?

JC> The below functions offer the equivalent of list comprehensions with a
JC> final post-processing step.

Well, what I was suggesting is not just a cross of two lists but the
syntax sugar which would make statements more consistent to the
generators/comprehensions and also give some new opportunities.
I think that my large proposal can be splitted to several
almost-independent ones, each carrying separate features, able to
be implemented independently and worth independent reconsidering:

1. Bring 'if'-s from generator/comprehension 'for' syntax to 'for'
statement. That's truly inconsistent that one may write

 list2 = [i for i in list if cond(i)]

but cannot write

 for i in list if cond(i):

2. Bring "several for-s in a row" ability from
generators/comprehensions to the statement (and expand it to "several
for-s and if-s", of course). We can write

 list2 = [f(i, j) for i in list1 for j in list2]

but cannot write

 for i in list1 for j in list2:
    yield f(i, j)

That looks inconsistent as well.

Yes, for this little case we could do some kind of cross, but what if
there are more loops, and/or some of them require filtering by if-s?

3. Bring 'while' into the loops, both statements and iterators. Now no
need to worry about "hidden-goto" 'break', especially for the cases of
nested loops, while it can be easily checked right inside the looping
condition? Now we can easily do

 for i in largelist while !found:

, isn't it nice and simple?

С уважением,
 Alexander                          mailto:maa_public at sinn.ru

More information about the Python-Dev mailing list