Okay, so is there a way to return a value from a function - the way return
does - but not stop the function - the way return
does?
I think you are looking for yield
. Example:
import time
def myFunction(limit):
for i in range(0,limit):
time.sleep(2)
yield i*i
for x in myFunction(100):
print( x )
def f():
for i in range(10):
yield i
g = f().next
# this is if you actually want to get a function
# and then call it repeatedly to get different values
print g()
print g()
print
# this is how you might normally use a generator
for i in f():
print i
Output:
0
1
0
1
...
9