[Python-ideas] Statements vs Expressions... why?
Cliff Wells
cliff at develix.com
Tue Sep 16 04:00:45 CEST 2008
On Tue, 2008-09-16 at 13:06 +1200, Greg Ewing wrote:
> Cliff Wells wrote:
>
> > s = 'abc'
> > x = for c in s: YIELD c # x = 'a', 'b', 'c'
>
> You mean that the value of (for c in s: YIELD c) is
> a tuple? Or that it's an iterator that produces that
> series of values?
The latter, although I admit not seeing the importance of the
distinction in this case.
> > 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' )
>
> I'm still not getting a clear idea of what semantics
> you intend for a for-loop-with-YIELD. Neither of the
> interpretations I suggested above (sequence or iterator)
> seems to produce this result.
>
> > maybe this is clearer as
> >
> > for J in I:
> > tmp = for j in J: YIELD j
> > YIELD tmp
>
> No, that's not any clearer. Can you provide a translation
> into current, valid Python?
My example was a translation of Arnaud's challenge:
I = ['spam', 'eggs']
def flatten(I):
for J in I:
for j in J:
yield j
>>> '-'.join(flatten(['spam', 'eggs']))
's-p-a-m-e-g-g-s'
It's possible there's a nuance you're seeing that I'm not (and
unfortunately we can't test). What are you predicting as the output?
Cliff
More information about the Python-ideas
mailing list