-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDiffBtwnGenratorsAndIterator.py
63 lines (43 loc) · 1.75 KB
/
DiffBtwnGenratorsAndIterator.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
58
59
60
61
62
63
__author__ = 'Sanjay'
# Differences between Generator function and a Normal Iterator function
# Here is how a generator function differs from a normal function.
#
# Generator function contains one or more yield statement.
# When called, it returns an object (iterator) but does not start execution immediately.
# Methods like __iter__() and __next__() are implemented automatically. So we can iterate through the items using next().
# Once the function yields, the function is paused and the control is transferred to the caller.
# Local variables and their states are remembered between successive calls.
# Finally, when the function terminates, StopIteration is raised automatically on further calls.
def my_gen():
n = 1
print('This is printed first')
# Generator function contains yield statements
yield n
n += 1
print('This is printed second')
yield n
n += 1
print('This is printed at last')
yield n
a = my_gen()
print (a)
next(a) # This is printed first
next(a) # This is printed second
next(a) # This is printed at last
# next(a) # StopIteration because there's no more n value available to bring out.
# One interesting thing to note in the above example is that, the value of variable n is remembered between each call.
#
# Unlike normal functions, the local variables are not destroyed when the function yields. Furthermore, the generator object can be iterated only once.
#
# To restart the process we need to create another generator object using something like a = my_gen().
# Note: One final thing to note is that we can use generators with for loops directly.
# Here you go ..
for item in my_gen():
print (item)
# Here's the output
# This is printed first
# 1
# This is printed second
# 2
# This is printed at last
# 3