Generators are used to create iterators, but with a different approach. Generators are simple functions which return an iterable set of items, one at a time, in a special way.
Mainly two concepts:
Generator-Function : A generator-function is defined like a normal function, but whenever it needs to generate a value, it does so with the yield keyword rather than return. If the body of a def contains yield, the function automatically becomes a generator function.The generator function can generate as many values (possibly infinite) as it wants, yielding each one in its turn.
Here is how a generator function differs from a normal function.
__next__()are implemented automatically. So we can iterate through the items using
StopIterationis raised automatically on further calls.
Here is an example to illustrate all of the points stated above. We have a generator function named
my_gen() with several
# A generator function that yields 1 for first time, # 2 second time and 3 third time def my_gen(): yield 1 yield 2 yield 3 # Driver code to check above generator function for val in my_gen(): print(val)
1 2 3
Generator-Object : Generator functions return a generator object. Generator objects are used either by calling the next method on the generator object or using the generator object in a “for in” loop.
Example: Implement a sequence of power of 2's using Generator function.
def PowerGen(max = 0): p = 0 while p < max: yield 2 ** p p += 1
Example: Generate all the even numbers
def all_even(): n = 0 while True: yield n n += 2
We take the vision which comes from dreams and apply the magic of science and mathematics, adding the heritage of our profession and our knowledge to create a design.