if in for loop

Ryan Lowe ryanlowe0 at msn.com
Tue Aug 19 23:30:35 EDT 2003


"Sean Ross" <sross at connectmail.carleton.ca> wrote in message
news:igA0b.4602$HB4.657643 at news20.bellglobal.com...
> "Ryan Lowe" <ryanlowe0 at msn.com> wrote in message
> news:rkz0b.126472$_R5.47133670 at news4.srv.hcvlny.cv.net...
> > i thought id ask here before wirting a PEP, if people thought it would
be
> a
> > good enhancement to allow if clauses in regular for-statements like so:
>
> Hi.
> This was brought up in July:
>
http://groups.google.ca/groups?hl=en&lr=&ie=UTF-8&selm=840592e1.0307021036.5
08d7d7d%40posting.google.com
>
> Here's the link for the entire thread:
>
http://groups.google.ca/groups?hl=en&lr=&ie=UTF-8&threadm=840592e1.030702103
6.508d7d7d%40posting.google.com&rnum=1&prev=/groups%3Fhl%3Den%26lr%3D%26ie%3
DUTF-8%26selm%3D840592e1.0307021036.508d7d7d%2540posting.google.com
>
> This was my response then (and now):
>
http://groups.google.ca/groups?hl=en&lr=&ie=UTF-8&selm=LUYMa.5841%24eF3.7215
51%40news20.bellglobal.com
>
> Other opinions will differ. (For instance, Michele Simionato was +1 on the
> idea)
>
>
> -0
>
> Sean Ross



>> >>> for x in y if x < 10 :
>>
>> is semantically equivalent to
>>
>> >>> for x in [x for x in y if x < 10] :
>>


well ok, so it wasnt a unique idea. but hey, if multiple people think the
same thing up independently, maybe theres something there...
i like list-comprehensions, they add a lot to the language, but they do seem
sort of thrown in.
fixing the symmetry, as the july poster put it, is a good way to think about
it.

your complaint is this?

>(being symmetrical) we should also be able to write:
>
>>>> msgs = []
>>>> for x in words:
>...  for y in otherwords:
>...   msgs.append("%s %s"%(x,y))
>...
>>>> msgs
>['hello foo', 'hello bar', 'bonjour foo', 'bonjour bar']
>
>
>as follows:
>
>msgs = []
>for x in words for y in otherwords:
>    msgs.append("%s %s"%(x,y))

i agree its not as clear whats going on here as in the if example, but its
no less readable than the list-comprehension version, i.e. msgs = ["%s
%s"%(x,y) for x in words for y in otherwords]
whats the argument? should they do away with double for's in list-comp?


>It doesn't do anything for me. I actually find it harder to
>read: the brackets at least guide my eyes to the various parts
>of the line, which the form  you propose doesn't do.

>Also, its most definitely
>not backwards compatible, and I wish you wouldn't
>send three duplicate messages to the newsgroup with
>different headers but the same body.

**harder** to read!? cmon now. i will buy its certainly not a necessary
change, but harder to read...
all i meant by it being backwards compatible is that for statements without
the clause would work as they did before.
sorry about the triple post. i got error messages when i posted so i didnt
think they went through.

ryan











More information about the Python-list mailing list