We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent ba8b995 commit e9d9ef6Copy full SHA for e9d9ef6
1 file changed
py3/advance/do_generator.py
@@ -1,9 +1,9 @@
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
4
-g = (x * x for x in range(10))
5
-print(g)
6
-for x in g:
+s = (x * x for x in range(5))
+print(s)
+for x in s:
7
print(x)
8
9
def fib(max):
@@ -12,8 +12,29 @@ def fib(max):
12
yield b
13
a, b = b, a + b
14
n = n + 1
15
+ return 'done'
16
17
f = fib(10)
18
print('fib(10):', f)
19
for x in f:
20
21
+
22
+# call generator manually:
23
+g = fib(5)
24
+while 1:
25
+ try:
26
+ x = g.send(None)
27
+ print('g:', x)
28
+ except StopIteration as e:
29
+ print('Generator return value:', e.value)
30
+ break
31
32
+# call generator using iter:
33
+i = iter(fib(5))
34
35
36
+ r = next(i)
37
+ print('i:', r)
38
39
40
0 commit comments