if in for loop

Sean Ross sross at connectmail.carleton.ca
Wed Aug 20 11:16:04 EDT 2003


"Ryan Lowe" <ryanlowe0 at msn.com> wrote in message
news:vXB0b.126774$_R5.47942299 at news4.srv.hcvlny.cv.net...
> your complaint is this?
[snip code example]
> 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?


Actually, I hadn't really formulated an argument. I'm indifferent to the
proposal, with some slight leaning towards non-acceptance (as my "-0" vote
suggests). I don't think I can justify why I don't care for the proposal. It
does seem reasonable. If your goal is to maintain consistency within the
language, then this proposal would seem to aid that goal. I also prefer that
a language provide consistency. The problem, I suppose, should you choose to
be consistent, is deciding in which direction to focus that consistency.
While I very much enjoy list comprehensions, and would prefer that they not
be removed from the language (read, "Never remove them from the language,
please!" and "Could we please have generator comprehensions, as well?"), I
also see that they are somewhat of an anomaly and that they can hinder
readability when they are allowed to grow without bound. Of course, the same
can be said for deeply nested for-loops - at some point, as you add more and
more for-loops, the meaning becomes less and less clear (which would be one
reason people refactor such things using functions). Anyway. I suppose the
question I was addressing in the earlier post was this: Does consistency, in
this instance, aid or hinder, and what does it aid or hinder. If we argue
that filtering should be added to the for-loop syntax because that syntax
should be more consistent with the list comprehension syntax,  then it seems
reasonable to also argue for the inclusion of multiple for-expressions, on
the same basis (Is this argument fallacious? I'm not sure...looks like a
slippery-slope) . So, anticipating that others might make this argument once
one part of list comprehension syntax had been retro-fitted into the
existing for-loop syntax, it seemed reasonable to ask - do you also think
multiple for-expressions in a for-loop would be of benefit? Do you want only
the one and not the other? (False dichotomy?) If so, why? If not, why not?
These are topics you may wish to address, were you to write a PEP. As I've
said, I'm pretty much neutral towards the whole thing, with some tendency
towards caution.

[snipped quote from John Roth posting]
> **harder** to read!? cmon now. i will buy its certainly not a necessary
> change, but harder to read...

for x in X if condx(x) for y in Y if condy(y) for z in Z if condz(z) for w
in W if condw(w):
    suite

for x in X:
    if condx(x):
        for y in Y:
             if condy(y):
                 for z in Z:
                      if condz(z):
                           for w in W:
                               if condw(w):
                                   suite

Neither strikes me as being particularly easier to read than the other.

for x in X if condx(x):
    suite

for x in X:
    if condx(x):
        suite


Same thing here.








More information about the Python-list mailing list