Python Tutorial

Sunday, October 7, 2012

Python keyword: Yield



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


Output:
0
1
2
3
4

0 comments:

Post a Comment