How to define Function Generators in Python Program
In Python, a Generator is a special routine that can be used to control the iteration behavior of a loop. A generator is similar to a function returning an array. A generator will allow the parameters and can generates a sequence of numbers. Unlike functions, which return a whole array, the generator can yields one value at a time which takes less memory. The Generator function will use a keyword “yield” to return the values.
Generally, the functions starts execution from first line and continues until we reaches the return statement or an exception or end of the function. Also scope of the local variables created during the function scope are destroyed and not accessible further.
While in case of Generator when it encounters a 'yield' keyword, the state of the function will be frozen and all the variables are stored in memory until the generator is called again.
We can use the generator in accordance with an iterator or can be explicitly called by using the “next” keyword.
Example 1:
# The generator function to return a sequence from 'a' to 'b'
def func_gen(a, b):
while a <= b:
yield a
a = a + 1
# calling the generator function with parameter values
gen = func_gen(3,9)
lst = []
print('The sequence of numbers is:')
for i in gen:
lst.append(i) # storing the elements in a list
print(i, end=' ')
# storing the reslult of generator into a list
print('\nThe list of elements from generator is:\n', lst)
# Output:
The sequence of numbers is:
3 4 5 6 7 8 9
The list of elements from generator is:
[3, 4, 5, 6, 7, 8, 9]
# Retrieving the element by element from a generator using next()
gen = func_gen(3, 9)
print(next(gen))
print(next(gen))
print(next(gen))
print(next(gen))
print(next(gen))
print(next(gen))
print(next(gen))
print(next(gen))
# Result:
3
4
5
6
7
8
9
Once it completes all the elements, if you still try to call the next iteration, then it will return an error:
Traceback (most recent call last):
File "C:\Users\write\PycharmProjects\pythonProject\PyLab1\Lab1.py", line 69, in <module>
print(next(gen))
StopIteration
# ------------------------------------------------------------------------------------------------------------------------ #
Example 2: Retrieving the elements of a generator using '__next__'
# defining a Generator function
def func_gen_iter():
yield 'xyz'
yield 246
yield 40.50
gen = func_gen_iter()
# Accessing each element one by one from generator
print(gen.__next__())
print(gen.__next__())
print(gen.__next__())
print(gen.__next__())
# Result:
xyz
246
40.5
Once it completes all the elements, if you still try to call the next iteration, then it will return an error:
Traceback (most recent call last):
File "C:\Users\write\PycharmProjects\pythonProject\PyLab1\Lab1.py", line 83, in <module>
print(gen.__next__())
StopIteration
#--------------------------------------------------------------Thanks--------------------------------------------------------------#
No comments:
Post a Comment
Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.