-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerators.py
57 lines (46 loc) · 1.05 KB
/
generators.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/python
# Date: 2018-09-09
#
# Description:
# How to create and use generator function.
print 'Simple generator function'
# yield is must for a function to become generator.
def simpleGeneratorFunction():
yield 1
yield 2
yield 3
print 'Access using loop: ',
for item in simpleGeneratorFunction():
print item,
x = simpleGeneratorFunction()
print '\nAccess using next(): ',
print x.next(),
print x.next(),
print x.next(),
# print x.next() # raises 'StopIteration' exception.
print '\n\nFibonacci generator function'
def fib(limit):
a, b = 0, 1
while a < limit:
yield a
a, b = b, a + b
print 'Access using for loop: ',
for n in fib(8):
print n,
x = fib(6)
print '\nAccess using next(): ',
print x.next(),
print x.next(),
print x.next(),
print x.next(),
print x.next(),
print x.next(),
# Output:
# ------------------------
# Simple generator function
# Access using loop: 1 2 3
# Access using next(): 1 2 3
#
# Fibonacci generator function
# Access using for loop: 0 1 1 2 3 5
# Access using next(): 0 1 1 2 3 5