python - Function that always keep returning new data -
i'm kind of new python , i'm trying run code program, i'm stuck in part need function keep returning new data, here's part of code keep returning zeros!
import time def forloop(): in range(0,10000): return while true: time.sleep(0.25) print(forloop())
when call forloop()
, return i
inside it, returns function, , next time call forloop()
start beginning. want use generator functions
you can use generator functions
(using yield
statement) yield values instead of return
.
example -
def forloop(): in range(0,10000): yield x = forloop() while true: try : time.sleep(0.25) print(next(x)) except stopiteration: x = forloop()
next(x)
throws stopiteration
exception if generator has been exhausted, in case catch exception , recreate generator.
Comments
Post a Comment