Python Yield keyword work as return, but it always return a generator. You can get the value/values by iterating the generator.
It is very usefull when you know your function will return a huge set of values that you will only need to read once
Code:
def myFun(n):
for i in range(0,n):
yield i
it=myFun(5)
for i in it:
print i
0 comments:
Post a Comment