Normal Functions vs Generator Functions:
Generators in Python are created just like how you create normal functions using the ‘def’ keyword. But, Generator functions make use of the yield keyword instead of return. This is done to notify the interpreter that this is an iterator. Not just this, Generator functions are run when the next() function is called and not by their name as in case of normal functions. Consider the following example to understand it better:
EXAMPLE:
|
def func(a):
yield a
a=[1,2,3]
b=func(a)
next(b) |
OUTPUT: [1, 2, 3]
As you can see, in the above output, func() is making use of the yield keyword and the next function for its execution. But, for normal function you will need the following piece of code:
EXAMPLE:
|
def func(a):
return a
a=[1,2,3]
func(a) |
OUTPUT: [1, 2, 3]