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 ???
ser()creates the generator. So each time you callser()it is sending you a new generator instance. You need to use it just like the expression: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.