The functions generally follow the conventional process flow, return values and quit. Generators work similarly, but remember the state of the processing between calls, staying in memory and returning the next item expected when activated.
The generators have several advantages over conventional functions:
Generators are usually called through a for loop. The syntax is similar to the traditional function, just the yield instruction substitutes return. In each new iteraction, yield returns the next value.
Exemple:
In [1]:
def gen_pares():
"""
Generates even numbers from 0 to 20
"""
i = 0
while i <= 20:
yield i
print("dada")
i += 2
# Shows each number and goes to the next
for n in gen_pares():
print ("> ", n)
In [4]:
def gen_pares():
"""
Generates even numbers from 0 to 20
"""
i = 0
yield i
print("dada")
i += 2
yield i
# Shows each number and goes to the next
for n in gen_pares():
print ("> ", n)
In [6]:
a = list(gen_pares())
print(a)
Another example:
In [1]:
import os
# Finds files recursively
def find(path='.'):
for item in os.listdir(path):
fn = os.path.normpath(os.path.join(path, item))
if os.path.isdir(fn):
for f in find(fn):
yield f
else:
yield fn
# At each interaction, the generator yeld a new file name
for fn in find(r"/home/mayank/code/mj/raas/test/"):
print (fn)
In [3]:
import time
import sys
def fib():
a, b = 0, 1
while True:
yield b
d = a + b
if d > 20:
break
a, b = b, d
iter = fib()
try:
for i in iter:
print( i),
time.sleep(1)
sys.stdout.flush()
print("...Done...")
except KeyboardInterrupt:
print( "Calculation stopped")