I was recently asked to do this task (school) :
Write a loop generator, which takes as parameter a finite iterable, and generates in infinite loop the iterable
So I did :
import itertools
def loop(l):
for element in itertools.cycle(l):
yield element
and one of my classmate did :
def loop(l):
while True:
for element in l:
yield element
I'd like to know what are the main differences between the two and if there is a more "pythonic" way to write something simple as this.