I know I must be missing something simple, but I am not seeing it.
If I have a generator expression like this:
>>> serializer=(sn for sn in xrange(0,sys.maxint))
I can generate, easily, individual integers like this:
>>> serializer.next()
0
>>> serializer.next()
1
>>> serializer.next()
2
If I write a generator like this:
>>> def ser():
... for sn in xrange(0,100000):
... yield sn
It is no bueno:
>>> ser().next()
0
>>> ser().next()
0
>>> ser().next()
0
??? What am I missing ???
The next() function returns the next item in an iterator. You can add a default return value, to return if the iterable has reached to its end.
If you want to return multiple values from a function, you can use generator functions with yield keywords. The yield expressions return multiple values. They return one value, then wait, save the local state, and resume again.
The yield keyword pauses generator function execution and the value of the expression following the yield keyword is returned to the generator's caller. It can be thought of as a generator-based version of the return keyword.
Unless your generator is infinite, you can iterate through it one time only. Once all values have been evaluated, iteration will stop and the for loop will exit. If you used next() , then instead you'll get an explicit StopIteration exception.
ser()
creates the generator. So each time you call ser()
it is sending you a new generator instance. You need to use it just like the expression:
serializer = ser()
serializer.next()
Consider that, if it didn't work this way, you could only ever use the ser()
function once and you could never reset it. Plus, you can change the ser function to accept a max integer, and make your program more flexible.
def ser(n=sys.maxint):
for sn in xrange(0, n):
yield sn
In your second example you keep creating a new instance, starting with a fresh generator.
g = ser()
g.next()
g.next()
Create the generator once and reuse it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With