if in for loop
ryanlowe0 at msn.com
Wed Aug 20 05:30:35 CEST 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
> > good enhancement to allow if clauses in regular for-statements like so:
> This was brought up in July:
> Here's the link for the entire thread:
> This was my response then (and now):
> Other opinions will differ. (For instance, Michele Simionato was +1 on the
> 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
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))
>['hello foo', 'hello bar', 'bonjour foo', 'bonjour bar']
>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.
More information about the Python-list