'Create an infinite generator function

I am trying to code a generator, which, once emptied, starts again with the initial input. Example:

hi = cycle("Hi")
for v in range(5):
    print(v)

returns

H
i
H
i
H

I have been playing around with copies and try / except statements. For example:

def cycle(a):
    
    b = copy.deepcopy(a)
    try:
        for e in b:
            yield e
    except:
        cycle(a)

However, I am not even sure if I can call a generator function again in the generator or if the try block works as expected, since I always get a StopIteration Error regardless of the method I choose. Any tips or more importantly explanations why this is not working as intended?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source