>>> def make_counter(x):
... print 'entering make_counter'
... while 1:
... yield x
... print 'incrementing x'
... x = x + 1
...
>>> counter = make_counter(2)
>>> coutner
Traceback (most recent call last):
File "", line 1, in
NameError: name 'coutner' is not defined
>>> counter
>>> counter.next()
entering make_counter
2
>>> counter.next()
incrementing x
3
>>> counter.next()
incrementing x
4
The presence of the yield keyword in make_counter means that this is not a normal function. It is a
special kind of function which generates values one at a time. You can think of it as a resumable function.
Calling it will return a generator that can be used to generate successive values of x.
To create an instance of the make_counter generator, just call it like any other function. Note that this does
not actually execute the function code. You can tell this because the first line of make_counter is a print
statement, but nothing has been printed yet.
The make_counter function returns a generator object.
The first time you call the next() method on the generator object, it executes the code in make_counter
up to the first yield statement, and then returns the value that was yielded. In this case, that will be 2, because
you originally created the generator by calling make_counter(2).
Repeatedly calling next() on the generator object resumes where you left off and continues until you hit the
next yield statement. The next line of code waiting to be executed is the print statement that prints
incrementing x, and then after that the x = x + 1 statement that actually increments it. Then you loop
through the while loop again, and the first thing you do is yield x, which returns the current value of x
(now 3).
Using generators instead of recursion
def fibonacci(max):
a, b = 0, 1
while a < max:
yield a
a, b = b, a+b
for n in fibonacci(100000):
print n,
You can use a generator like fibonacci in a for loop directly. The for loop will create the generator
object and successively call the next() method to get values to assign to the for loop index variable (n).
Each time through the for loop, n gets a new value from the yield statement in fibonacci, and all you do
is print it out.
... print 'entering make_counter'
... while 1:
... yield x
... print 'incrementing x'
... x = x + 1
...
>>> counter = make_counter(2)
>>> coutner
Traceback (most recent call last):
File "
NameError: name 'coutner' is not defined
>>> counter
>>> counter.next()
entering make_counter
2
>>> counter.next()
incrementing x
3
>>> counter.next()
incrementing x
4
The presence of the yield keyword in make_counter means that this is not a normal function. It is a
special kind of function which generates values one at a time. You can think of it as a resumable function.
Calling it will return a generator that can be used to generate successive values of x.
To create an instance of the make_counter generator, just call it like any other function. Note that this does
not actually execute the function code. You can tell this because the first line of make_counter is a print
statement, but nothing has been printed yet.
The make_counter function returns a generator object.
The first time you call the next() method on the generator object, it executes the code in make_counter
up to the first yield statement, and then returns the value that was yielded. In this case, that will be 2, because
you originally created the generator by calling make_counter(2).
Repeatedly calling next() on the generator object resumes where you left off and continues until you hit the
next yield statement. The next line of code waiting to be executed is the print statement that prints
incrementing x, and then after that the x = x + 1 statement that actually increments it. Then you loop
through the while loop again, and the first thing you do is yield x, which returns the current value of x
(now 3).
Using generators instead of recursion
def fibonacci(max):
a, b = 0, 1
while a < max:
yield a
a, b = b, a+b
for n in fibonacci(100000):
print n,
You can use a generator like fibonacci in a for loop directly. The for loop will create the generator
object and successively call the next() method to get values to assign to the for loop index variable (n).
Each time through the for loop, n gets a new value from the yield statement in fibonacci, and all you do
is print it out.
Comments
Post a Comment
https://gengwg.blogspot.com/