Return to Snippet

Revision: 38899
at January 10, 2011 01:48 by magicrebirth


Initial Code
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))

Initial URL
http://docs.python.org/release/2.3/whatsnew/section-generators.html

Initial Description


Initial Title
Python generator

Initial Tags
python

Initial Language
Python