如何理解函數repeater2
和repeater3
?關於Python生成器
def repeater1(value):
new = (yield value)
def repeater2(value):
while True:
new = (yield value)
def repeater3(value):
while True:
new = (yield value)
if new is not None:value = new
r1,r2,r3 = repeater1(1),repeater2(2),repeater3(3)
r1.next() #1
r2.next() #2
r3.next() #3
r1.send(4) #StopIteration
r2.send(4) #2
r2.next() #2
r3.send(4) #4
r3.next() #4
'new = field value'是無效的python。這段代碼似乎有一堆遺漏。 – pvg
你是否意外地混淆了'field'和'yield'? –
@KlausD。 Ooo我很抱歉, – Alex