yieldについて
yieldはイテレータを返すと覚えていたけど、
StopIterationまで行くと破棄されるみたい?
余り意識していなかったので下のような例に違和感があった。
>>> def test4(): ... a = iter([1,2,3]) # ちょっと違和感ある所 ... while True: ... yield next(a) ... >>> ts4 = test4() >>> next(ts4) 1 >>> next(ts4) 2 >>> next(ts4) 3 >>> next(ts4)
こうすると終わりが無いのね。
>>> def test4(): # これだと終端の無いイテレータになる ... while True: ... yield 1 ... >>> ts4 = test4() >>> next(ts4) 1 >>> next(ts4) 1 >>> next(ts4) 1 >>> next(ts4) # 永久に続くのだろう。。。