/ Published in: Python
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
def generate_ints(N): for i in range(N): yield i >>> gen = generate_ints(3) >>> gen <generator object at 0x8117f90> >>> gen.next() 0 >>> gen.next() 1 >>> gen.next() 2 >>> gen.next() Traceback (most recent call last): File "stdin", line 1, in ? File "stdin", line 2, in generate_ints StopIteration # another option: a, b, c = generate_ints(3) # or.. list_of_three = list(generate_ints(3))
URL: http://docs.python.org/release/2.3/whatsnew/section-generators.html