[Python] yield
2016-03-06 本文已影响11人
何幻
def test_gen():
v = yield 'a'
print(v) #2. None
v = yield 'b'
print(v) #4. None
v = yield 'c'
print(v) #6. e
v = yield 'd'
print(v) #8. None
iter = test_gen()
print(iter.__next__()) #1. a
print(next(iter)) #3. b
print(iter.send('e')) #5. c
for v in iter:
print(v) #7. d
注:
iter.__next__()
相当于iter.send(None)