[Python-ideas] Statements vs Expressions... why?

Greg Ewing greg.ewing at canterbury.ac.nz
Mon Sep 15 04:11:48 CEST 2008

Cliff Wells wrote:

> Not that it's terribly relevant to what you say, but:
> '-'.join ( 
>      for J in I: YIELD ( for j in J: YIELD j ) 
> )

No, I think you were right the first time. The above
looks like it will generate a sequence of iterators,
not a flat sequence of j values.

> x = [ J [ a ] 
>       for a in b 
>       if c ]
> vs
> x = for a in b: 
>         if c: continue J [ a ] 
> The complexity has barely increased and yet the second form is already
> more readable

That's a matter of opinion. If you lay out the first one as

   x = [ J [ a ]
            for a in b if c ]

there's not much difference between them.

> Not to mention, the listcomp's placement of the
> yielded result before the loop and condition only works well when it's a
> very simple expression.  It doesn't scale well with complexity (not that
> I think it was meant to).

You're right, it's not meant to. It's meant for the simple
cases where all the syntactic overhead of a full for-statement
and list appending code swamps the content. Given that, adding
any extra syntactic baggage, even just a 'continue' keyword,
reduces its effectiveness for its intended purpose.

Also, I think it reads quite nicely with the expression at
the beginning. It's modelled after the mathematical notation
for describing sets:

   {x : some conditions on x}


More information about the Python-ideas mailing list