-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclose_coroutines.py
More file actions
34 lines (28 loc) · 809 Bytes
/
close_coroutines.py
File metadata and controls
34 lines (28 loc) · 809 Bytes
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
def counter(maximum):
initial = 0
while initial < maximum:
value = yield initial # equals to None till .send(number) is called
# If value is given (remember default is None) then change the counter
if value is not None:
initial = value
else:
initial += 1
c = counter(10)
for i in c:
print(i)
if i == 5:
c.close()
def print_name(prefix):
print("Search for", prefix, "prefix")
try:
while True:
name = yield
if prefix in name:
print(name)
except GeneratorExit:
print("Closing generator!")
if __name__ == "__main__":
pn = print_name("Dear")
next(pn) # calls first yield expression
pn.send("Alex")
pn.send("Dear Alex") # matches with prefix