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

Cliff Wells cliff at develix.com
Tue Sep 16 02:31:28 CEST 2008


On Tue, 2008-09-16 at 11:41 +1200, Greg Ewing wrote:
> Cliff Wells wrote:
> > On Mon, 2008-09-15 at 14:11 +1200, Greg Ewing wrote:
> >>Cliff Wells wrote:
> >>>
> >>>'-'.join ( 
> >>>     for J in I: YIELD ( for j in J: YIELD j ) 
> >>>)
> >
> > join() takes care of flattening the final yielded iterator.
> 
> But the final iterator is yielding other iterators,
> not strings, unless I misunderstand the semantics you
> have in mind.

I think so.  For the simplest case

s = 'abc'
x = for c in s: YIELD c     # x = 'a', 'b', 'c'

so for the previous example

I = [ 'spam', 'eggs' ]

for J in I:                 # J = 'spam' then 'eggs'
    YIELD (                 # evaluate to an iterable
        for j in J: YIELD j # j is 's' then 'p' then ...
    )

so we get '-'.join( 's','p','a','m','e','g','g','s' )

maybe this is clearer as

for J in I: 
    tmp = for j in J: YIELD j
    YIELD tmp


> > Get into nested listcomps and the
> > readability (or more to the point the comprehensibility) pretty much
> > vaporizes.
> 
> That depends on what you mean by "nested listcomps". I agree
> that nesting one entire listcomp inside another tends to
> look rather confusing:
> 
>    [f(a) for a in [g(b) for b in y]]
> 
> But that's not the same thing as having nested *loops*
> within a single listcomp, which I don't think is particularly
> bad at all:
> 
>    [f(a, b) for a in x for b in y]
> 
> or if you prefer,
> 
>    [f(a, b)
>      for a in x
>        for b in y]
> 
> > Sure, I think listcomps have a place.  I still maintain that they are
> > logically redundant if you have if-expressions (not to mention less
> > flexible)
> 
> Yes, but they're also logically redundant even if you don't
> have statement-expression equivalence, so that's not an argument
> for merging statements and expressions.

Well part of the problem this entire thread has suffered is that there
are several related issues being argued simultaneously.  Perhaps if I
were a better presenter this would have gone differently, but in any
case what I was trying to get across is that Python has grown lots of
extensions that could be considered redundant and will grow more unless
what I see as the seed for these desired extensions is addressed (I
believe it to be a desire for better FP support, which merged statements
and expressions would largely address).  I selected the ternary
if-operator, generators and listcomps as examples but I may have
overreached a bit.

Regards,
Cliff





More information about the Python-ideas mailing list