Closed
Description
In python 3.X yield is a statement (currently supported) but also an expression.
You can do something like this:
from typing import Iterator
def test_yield() -> Iterator[int]:
value = 1
try:
value = yield value # Here is the yield expression
yield value
yield value + 1
except TypeError as e:
print(e)
gen = test_yield()
x1 = next(gen)
x2 = gen.send(2)
print(x1) # 1
print(x2) # 2
gen.throw(TypeError, 'ERROR')
gen.close()
Right now, mypy complains in the expression:
_program.py: In function "test_yield":
_program.py, line 6: Parse error before "value"
And, also, the Iterator type need the functions send, throw and close, but I think this go in typing project:
_program.py, line 14: Iterator[int] has no attribute "send"
_program.py, line 17: Iterator[int] has no attribute "throw"
_program.py, line 18: Iterator[int] has no attribute "close"
The reference is here: https://docs.python.org/3.4/reference/expressions.html#yieldexpr