ジェネレーターについて、期待した出力が得られないという誤解は何ですか? . send() したデータを出力する単純な関数を作成しようとしています。情報が送信されない場合は「なし」を返します。
import pudb
#pudb.set_trace()
def gen():
i = 0
while True:
val = (yield i)
i = val
if val is not None:
yield val
else:
yield 'none'
test = gen()
next(test)
print test.send('this')
print test.send('that')
print test.next()
print test.send('now')
期待される出力:
'this'
'that'
'none'
'now'
実際の出力:
'this'
'this'
'none'
'none'