[Solved] Can I store an iterator in a file which I can read from later? Will this reduce space consumption? [closed]

Building on larsmans answer, a custom iterator can be built to do this: class my_large_num(object): def __init__(self): self.num_iterations = 0 def __iter__(self): return self def next(self): if self.num_iterations < 1: self.num_iterations += 1 return 10**200 else: raise StopIteration() You can then: import pickle pickled_repr = pickle.dumps(my_large_num()) restored_object = pickle.loads(pickled_repr) sum(restored_object) This works because underneath, iterable … Read more