Stopping an iterator and continuing later

Stefan Ram Stefan Ram Stefan Ram Stefan Ram nospam.nospam.ram at zedat.fu-berlin.de
Sat Nov 25 19:40:00 EST 2017


ram at zedat.fu-berlin.de (Stefan Ram) writes:
>Then you can use pickle or custom methods to save and
>restore the object, or get the state from an iterator
>and create a new iterator with that state later.

  One does not always have to write a custom class,
  for example:

  main.py

import pickle
r = range( 9 )
i = iter( r )
del r
next( i )
next( i )
next( i )
bytes = pickle.dumps( i )
del i
i = pickle.loads( bytes )
print( next( i ))
del i
del pickle

  transcript

3




More information about the Python-list mailing list