yield expression programmized-formal interpretation. (interpretation of yield expression.)

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Apr 22 03:32:08 CEST 2008


En Mon, 21 Apr 2008 15:03:05 -0300, <castironpi at gmail.com> escribió:

> What if I say
>
>    oath= yield
>
> or
>
>    other= yield
>
> ?
>
> Does yield evaluate without parenthes?  (Eth.)

You can't use yield except in a generator function. From  
<http://docs.python.org/ref/yieldexpr.html> and the grammar definition at  
<http://docs.python.org/ref/grammar.txt> you can see that

assignment_stmt ::=
              (target_list "=")+
               (expression_list | yield_expression)

yield_expression ::= "yield" [expression_list]

The last expression_list is optional so a bare yield should be allowed in  
the right hand side of an assignment, as if it were `yield None` (I think  
such behavior is specified in the original PEP). Let's try:

py> def gen():
...   x = yield
...   y = yield "second"
...   yield x, y
...
py> g = gen()
py> print g.next()
None
py> print g.send(123)
second
py> print g.send(456)
(123, 456)
py> print g.send(789)
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
StopIteration

-- 
Gabriel Genellina




More information about the Python-list mailing list