Saturday, June 4, 2016

python generators or python yield

Note: Infinite loop is not an issue for generator. It does not take processor time as the generator object is called only during data retrieval:
def my_gen():
    count = 0      
    while True:
        count += 1      
        yield count + 1

Usage: memory optimized iteration (virtually stores calculated values like list without actually consuming memory: the values are retrieved runtime: kind of creates a memory place holder at a place in memory where a calculation is required and then call whenever it is required) and asyncronous operation

# yield is more than return
return is meant to stop/exit function execution when return keyword is reached. the objective of yield is to return continued values indefinitely. usage of return may look same some time but not always. return is mostly used at the end of the function to return a value and end the function, while yield objective is to continue returning when the values are to be retrieved.
yield => "yield a series of values". Function with yield is called generator because it is used to generate a series of values.
-return gets only once chance to return a value
-For infinite series, if we use return - we have to store all the values in a variable which may be out of memory or impossible to hold : but if the function can return one value at a time by doing all the calculation - then we can deal with very large or infinite numbers. - there is no more memory issue with this approach.

IMPORTANT: During 1st element retrieval, all the lines of the function is called, then (2nd onward only yield (kind of return) values are returned.

http://stackoverflow.com/questions/7883962/where-to-use-yield-in-python-best
http://pymbook.readthedocs.io/en/latest/igd.html
https://jeffknupp.com/blog/2013/04/07/improve-your-python-yield-and-generators-explained/

yield is best used when you have a function that returns a sequence and you want to iterate over that sequence, but you do not need to have every value in memory at once.

yield with respect to coroutines, cooperative multitasking and asynchronous I/O

# About generator:
-a function becomes generator when return is replaced by keyword 'yield'
-when a generator is called first time, it creates a object in memory and does not execute and code in the generator or generator function.
-To use generator

# Simple generator example:
def a():
    yield 1

b = a()  # generator initialized: a generator cannot be used without initialization like class. Generator initialization look like function call - but both (function and generator) works/behaves differently.

b.next()  # retrieving values expected from yield. Once a value is retrieved, the generator object points to next element and the object looses that element after retrieval.

# Understand memory optimized operation using yield
- Often we need to collect collected values in a variable by calling a function and use that data into another function or another place in the script.

Example:
-a = call_function_that_gives_a_list(args1)
-b1 = call function_1( args1)
-b2 = call function_2( args2)

# One use of generator
def get_primes(number):
    while True:
        if is_prime(number):
            yield number
        number += 1

No comments:

Post a Comment