PlainChar appears in Python
Tonyuのほかにコルーチンをサポートする言語の代表格として,まずPythonをあげて,Tonyuと同じことをさせてみる.
from string import Template temp=Template("($x,$y)"); class PlainChar: def __init__(self, x, y): self.x=x; self.y=y; def draw(self): print temp.substitute(x=self.x,y=self.y); def move(self): while 1: self.x=self.x+1 yield 0 p1=PlainChar(30,20); p2=PlainChar(10,50); proc1=p1.move(); proc2=p2.move(); for a in range(10): proc1.next(); proc2.next(); p1.draw(); p2.draw();
ちゃんと並行動作する.
気になるのはこの部分.
def move(self): while 1: self.x=self.x+1 yield 0
Tonyuだとupdateと書くところだが,これに倣って
def update(self) yield 0 def move(self): while 1: self.x=self.x+1 self.update()
などと定義しようものなら,無限ループになってしまうのだ.
Tonyuなら,サブルーチン内でupdateしてもそれなりに振舞ってくれるのだが,そういうのは無理?