1# a generator that closes over outer variables
2def f():
3    x = 1 # closed over by g
4    def g():
5        yield x
6        yield x + 1
7    return g()
8for i in f():
9    print(i)
10
11# a generator that has its variables closed over
12def f():
13    x = 1 # closed over by g
14    def g():
15        return x + 1
16    yield g()
17    x = 2
18    yield g()
19for i in f():
20    print(i)
21
22# using comprehensions, the inner generator closes over y
23generator_of_generators = (((x, y) for x in range(2)) for y in range(3))
24for i in generator_of_generators:
25    for j in i:
26        print(j)
27
28# test that printing of closed-over generators doesn't lead to segfaults
29def genc():
30    foo = 1
31    repr(lambda: (yield foo))
32genc()
33