<jupyter_start><jupyter_text>Generators---<jupyter_code>def fib(a=1, b=1):
    while True:
        yield a; #-------------> give controll back to caller and continues on next()
        a, b = b, a + b
for idx, x in enumerate(fib()):
    if idx == 5: break
    print(x)
def fib_2(a=1, b=1):
    rv = []
    for _ in range(5):
        rv.append(a)   #---------> wait until all iterations
        a, b = b, a + b
    return rv
fib_2()<jupyter_output><empty_output><jupyter_text>**Consider a hypothetical situation you need the call the below 3 functions in order but it is not in your hand to make it possible. We can achieve this with a generator**<jupyter_code>class SomeClass:
    def first_function(self):
        return 'the first'

    def second_function(self):
        return 'the second'
    
    def third_function(self):
        return 'the third'
def the_gen():
    cls = SomeClass()
    yield cls.first_function()
    yield cls.second_function()
    yield cls.third_function()
gn = the_gen()
next(gn)
next(gn)
next(gn)
next(gn) # StopIteration: no more values to produce<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12792792792792793, 'fraction_numerical': 0.008108108108108109, 'mean_word_length': 2.982078853046595, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '586773', 'license': 'Apache-2.0', 'n_tokens_mistral': 377, 'n_tokens_neox': 367, 'n_words': 120, 'path': 'generators.ipynb', 'repo_name': 'suru33/py-notebooks', 'size': '1110'}