Singleton python generator? Or, pickle a python generator?

前端 未结 6 1326
心在旅途
心在旅途 2021-01-03 06:58

I am using the following code, with nested generators, to iterate over a text document and return training examples using get_train_minibatch(). I would like to

6条回答
  •  耶瑟儿~
    2021-01-03 07:27

    You can create a standard iterator object, it just won't be as convenient as the generator; you need to store the iterator's state on the instace (so that it is pickled), and define a next() function to return the next object:

    class TrainExampleIterator (object):
        def __init__(self):
            # set up internal state here
            pass
        def next(self):
            # return next item here
            pass
    

    The iterator protocol is simple as that, defining the .next() method on an object is all you need to pass it to for loops etc.

    In Python 3, the iterator protocol uses the __next__ method instead (somewhat more consistent).

提交回复
热议问题