yield statement: defining a generator function, : Generator : Buildin Function : Python examples (example source code) Organized by topic

C++
PHP
Python


Python  »  Buildin Function   » [  Generator  ]  Screenshots 
 



yield statement: defining a generator function,




def gensquares(N):
     for i in range(N):
         yield i ** 2               # resume here later

for i in gensquares(5):             # resume the function 
     print i, ':',                  # print last yielded value


x = gensquares(10)

print x.next()
print x.next()
print x.next()

           
       
Related examples in the same category
1.  Fibonacci sequences using generators Fibonacci sequences using generators
2.  yield statement works with for loop yield statement works with for loop
























Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.