Skip to content

Latest commit

 

History

History
40 lines (31 loc) · 732 Bytes

File metadata and controls

40 lines (31 loc) · 732 Bytes
>>> def spam_generator():
...     print('a')
...     yield 'spam'
...     print('b')
...     yield 'spam!'
...     print('c')
...     yield 'SPAM!'
...     print('d')
>>> generator = spam_generator()
>>> next(generator)
a
'spam'
>>> next(generator)
b
'spam!'

>>> import string
>>> def print_character():
...     i = 0
...     while True:
...         print('Letter: %r' % string.ascii_letters[i])
...         i = (i + 1) % len(string.ascii_letters)
...         yield
>>> # Always initialize
>>> print_character = print_character()
>>> next(print_character)
Letter: 'a'
>>> next(print_character)
Letter: 'b'
>>> next(print_character)
Letter: 'c'