summaryrefslogtreecommitdiffstatshomepage
path: root/tests/basics/generator_send.py
blob: c472c31ba937f42f1fa1370dcd1a447e62ade68d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def f():
    n = 0
    while True:
        n = yield n + 1
        print(n)

g = f()
try:
    g.send(1)
except TypeError:
    print("caught")

print(g.send(None))
print(g.send(100))
print(g.send(200))


def f2():
    print("entering")
    for i in range(3):
        print(i)
        yield
    print("returning 1")
    print("returning 2")

g = f2()
g.send(None)
g.send(1)
g.send(1)
try:
    g.send(1)
except StopIteration:
    print("caught")
try:
    g.send(1)
except StopIteration:
    print("caught")