[Python-Dev] PEP-xxx: Unification of for statement and list-comp syntax
Heiko Wundram
me+python-dev at modelnine.org
Mon May 22 06:56:44 CEST 2006
Am Montag 22 Mai 2006 01:59 schrieb Josiah Carlson:
> > 1) It unifies the syntax for list comprehensions and for loops, which use
> > the
>
> No, it /partially unifies/ list comprehensions and for loops. To
> actually unify them, you would need to allow for arbitrarily nested fors
> and ifs...
>
> for ... in ... [if ...] [for ... in ... [if ...]]*:
>
> If I remember correctly, this was why it wasn't accepted before; because
> actual unification is ugly.
This syntax is ugly, that's why the PEP doesn't try to make a case for this.
But, one level equivalence to list comprehensions isn't bad, again, at least
in my eyes.
> > 2) Just as I've replied to Terry J. Reed, if you find list comprehensions
> > easy to read, you're also bound to be able to understand what "for <expr>
> > in <expr> if <expr>:" does, at least AFAICT.
>
> Not everyone finds list comprehensions easy to read.
Why has Python added list-comprehensions, then? (or at least, why has Python
added the 'if'-expression to list-comprehensions if they're hard to read?
filter/itertools/any of the proposed "workarounds" in the PEP would also work
for list-comprehensions).
> > 3) Generally, indentation, as Terry J. Reed suggested, isn't always good.
> > If the body of the loop is more than a few lines long (which happens more
> > often than not in my code), extra indentation is bound to confuse me.
> > That's why I
>
> [snip]
>
> I feel for you; I really do. I've done the same thing myself. However,
> I don't believe that it is a good practice, in general, and I don't
> think that syntax should support this special case.
Why isn't this good practice? It's not always sensible to refactor loop code
to call methods (to make the loop body shorter), and it's a pretty general
case that you only want to iterate over part of a generator, not over the
whole content. Because of this, list comprehensions grew the 'if'-clause. So:
why doesn't the for-loop?
--- Heiko.
More information about the Python-Dev
mailing list