Can iterator be restored and can its value/status be assigned?

I have below snippet which use the generator to give the new ID


    def __init__(self, id_generator = None):
        if id_generator is None: id_generator = 0

        if isinstance(id_generator, int):
            import itertools
            self._generator = itertools.count(id_generator)

            self._generator = id_generator

    id =  #0
    id =  #1
    id =  #2
    id =  #3

my question is, suppose I have an existing id number such as int(99) need to be accepted during the runtime and need the _generator to generate the ID starting from 99. looks like:

             <---- new id number 99 be given in somehow

how I can feed it back to the _generator? Just use self._generator = itertools.count(99) or there's a better way?

More, sucn a confuse can be to be more generalized: how I can memo/assign a generator's status/value if it is not for an int type?



No, generators just generate items, you cannot set or save their state once they have been created. So self._generator = itertools.count(99) is really the best way to go.

What you can do is duplicate a generator with itertools.tee, which memorizes the output sequence from the first iterable and passes it to the new generators.

You can also write a generator that draws from a source you can change:

class counter(object):
    def __init__(self, current=0):
        self.current = current

    def __iter__(self):
        def iter():
            while True:
                yield self.current
                self.current += 1 # 
        return iter()

    def set(self,x):
        self.current = x

s = counter()
t = iter(s)
print # 0
print # 21

Need Your Help

C issue with connect function for TCP client

c sockets connect

I having a problem getting the connect function to work. This code will compile but when I run the program I'm getting an error that I'm passing Invalid arguments to connect. I have tried everythin...