>>> def gen_123(): ... yield 1 ... yield 2 ... yield 3 ... >>> gen_123 # doctest: +ELLIPSIS >>> gen_123() # doctest: +ELLIPSIS >>> for i in gen_123(): ... print(i) 1 2 3 >>> g = gen_123() >>> next(g) 1 >>> next(g) 2 >>> next(g) 3 >>> next(g) Traceback (most recent call last): ... StopIteration >>> def gen_AB(): ... print('start') ... yield 'A' ... print('continue') ... yield 'B' ... print('end.') ... >>> for c in gen_AB(): ... print('--->', c) ... start ---> A continue ---> B end.