>>> def make_counter(x):
print('entering make_counter')
while True:
yield x
print('incrementing x')
x = x + 1
>>> counter = make_counter(2)
>>> counter
>>> next(counter)
entering make_counter
2
>>> next(counter)
incrementing x
3
>>> next(counter)
incrementing x
4
>>> next(counter)
incrementing x
5
>>> def fib(max):
a,b = 0,1
while a < max: yield a a,b = b, a + b >>> for n in fib(1000):
print(n,end = ' ')
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
>>> list(fib(1000))
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987]
>>>
Python Generator の話