+ 2

Is there infinite 'for loop' in python?

Python 'for loop'.

12th Jan 2020, 7:40 PM
naveen maurya
naveen maurya - avatar
5 Réponses
+ 5
a = [0] for i in a: a.append(0)
12th Jan 2020, 7:44 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 5
You can use the ( while True )
12th Jan 2020, 8:29 PM
Mark Emad
Mark Emad - avatar
+ 4
You can use generators: def generate_infinitely(): while True: yield None for i in generate_infinitely(): print(i)
12th Jan 2020, 9:04 PM
Seb TheS
Seb TheS - avatar
+ 4
There are also infinite generators in itertools. from itertools import repeat for x in repeat(42): print(x)
12th Jan 2020, 9:14 PM
Tibor Santa
Tibor Santa - avatar
+ 3
You can also write a class with an iterator protocol, that never stops dishing out values. Running a for loop on an instance of that class would make it run forever. See here: https://code.sololearn.com/c2qlPZiFXHTL/?ref=app
12th Jan 2020, 10:33 PM
HonFu
HonFu - avatar