Python Generator's  and How to used them
Published: 2020-08-22 19:00:00

<!-- wp:heading -->
<h2>Agenda :</h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>What are Generators?</li><li>Normal function vs Generator </li><li>Advantages of using Generator</li><li>Generator with loops </li><li>Generator with function</li><li>Generator Expression</li><li>Use Cases</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":4} -->
<h4>what are  Generator in Python?</h4>
<!-- /wp:heading -->

<!-- wp:paragraph {"dropCap":true,"ampFitText":true} -->
<p class="has-drop-cap"><amp-fit-text layout="fixed-height" min-font-size="6" max-font-size="72" height="80">Generators are basically function that return the traversable object or items. Generator do not produce the output at once time but Generator produce the output when we need them. Generator has lots of advantages some of them we will discuss Here :)</amp-fit-text></p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":4} -->
<h4>Advantages of using Generator</h4>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>without the Generator in python producing the iterables is becomes very difficult and lenghty.</li><li>Generator easy to implement as they automatically implement is __iter()__ and __next()__ and <strong>stopiteration</strong></li><li>They can also used pipeline a numbe  of operations</li><li>can be used to produce an infinite number of items</li><li>Memory is saved as item is produced when we required unlike the in normal function. This facts becomes very important when you need to create a huge number of iterators.</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":4} -->
<h4>Normal Function  VS Generator Functions :</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The difference between Normal and Generator function is only keyword <strong><em>return</em></strong> to<em><strong> yield</strong></em>. in the normal function return the output but in the Generator function yield return the object of list . <em><strong>using next()</strong></em> we get the number using the object . in the Normal function we call the function by his function name but in the generator function we called them by <strong><em>next()</em></strong>.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"align":"center","level":5} -->
<h5 class="has-text-align-center">Generator Function</h5>
<!-- /wp:heading -->

<!-- wp:code -->
<pre class="wp-block-code"><code>#Genarator function
def func(a):
    yield a #change keyword return to yield
a = &#91;1,2,3]
b=func(a)
next(b)

output --->
&#91;1 , 2 , 3 ]</code></pre>
<!-- /wp:code -->

<!-- wp:heading {"align":"center","level":5} -->
<h5 class="has-text-align-center">using next()</h5>
<!-- /wp:heading -->

<!-- wp:code -->
<pre class="wp-block-code"><code>def myfunc(a):
    while a>=3:
        yield a
        a=a+1
b =  myfunc(a)
print(b)
next(b)</code></pre>
<!-- /wp:code -->

<!-- wp:heading {"align":"center","level":5} -->
<h5 class="has-text-align-center">Normal function</h5>
<!-- /wp:heading -->

<!-- wp:code -->
<pre class="wp-block-code"><code>def func(a):
    return a
a=&#91;1,2,3]
func(a)

output -->
&#91;1 ,2 , 3]</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>if you look the code above you will observer the  <strong><em>return</em></strong> change into the <strong><em>yield</em></strong> but their functional completly opposite. yield return the object of the output using next() and we get it . Normal function called by his name but Generator function called by next(). you will realize after see the below image.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":279,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://iffishells.files.wordpress.com/2020/08/new-generator.png?w=1024" alt="" class="wp-image-279"/></figure>
<!-- /wp:image -->

<!-- wp:heading -->
<h2>Generator with loops</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>In case you want to execute the same function at once, you can make use of the ‘for’ loop. This loop helps iterate over the objects and after all implementations it executes StopIteration.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>def z():
    n=1
    yield n
    n=n+3
    yield n
for x in z():
    print(x)


output --->>
1 
4</code></pre>
<!-- /wp:code -->

<!-- wp:heading -->
<h2>Generator Expression :</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>You can also use the expression along with the loop for loop procedure iterators. this makes the generation iterables and easy to understand.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>print("Generator Expression " , end= " :n ")
range_list=range(6)
ret =  (number for number in range_list)
print(c)
for ret_number in ret:
   print(ret_number)
</code></pre>
<!-- /wp:code -->

<!-- wp:heading -->
<h2>Major Difference </h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>As we said Generator makes the code pretty and readable and taking less memory and less code and as well as reduce  the code . now its time to look first i will write the code without the generator and then with generators then you will see the difference and blessing of the generator in python.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"align":"center","level":5} -->
<h5 class="has-text-align-center">Code without using Generator</h5>
<!-- /wp:heading -->

<!-- wp:code -->
<pre class="wp-block-code"><code>class Nums:
    MAX = 4
    def __init__(self):
        self.current = 0
    def __iter__(self):
        return self
    def __next__(self):
        next_value = self.current
        if next_value >= self.MAX:
            raise StopIteration
        self.current += 1
        return next_value
</code></pre>
<!-- /wp:code -->

<!-- wp:heading {"align":"center","level":5} -->
<h5 class="has-text-align-center">Code with Generator</h5>
<!-- /wp:heading -->

<!-- wp:code -->
<pre class="wp-block-code"><code>def func(var1):
    yield var1
    var1 +=1
</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->