Python disable while iterating
Question: So I am creating a data structure that is based on storage and memory. Lets
say I have the following method:

    
    
    def __store(self):
        #stores information into self.__memory list
    

now what I want to do is, if this function is called inside a loop, I want it
to be called **only after the loop is finished**

# My reasons

This method gets called in many other methods, most importantly `__setitem__`
so take a look at the following code:

    
    
    for i in xrange(100):
        class[i] = i + 5
    

right now, this will store the information up until the loop finished (100
times), but I only want it to store the information after the loop finishes.

# question

essentially I just need to be able to stop a function from running if
iteration is going on in the program, and then execute once the iteration is
done.

**how can I do that?**

# extra info

This data structure is a dictionary that has many functions for memory.

The kind of full-storage memory that I am storing with `__store` is for the
purpose of undoing . Anything in a loop would have no need for an undo
because, until the loop is over, the user should not need to undo anything.

This memory is based on control, so anything inside iteration should not be
stored because there is no need for the actual memory.

That is why I would not like to store during iteration, but, if something that
requires storage is run inside iteration, I would like it to run after the
iteration it was inside completes.


Answer: A function behave difference while inside a loop is a bad idea, try to use
`context manager` instead. it is a better way to do so and it is easier to
understand. <http://www.python.org/dev/peps/pep-0343/>

For example:

    
    
    with sqlite3.connect(":memory:") as conn:
        # update will hold in transaction.
        for i in xrange(100):
            conn.execute("insert ....")
    
    pass
    # sqlite3 will commit now
    

In the same way:

    
    
    with a() as b:
        # data won't store
        for i in xrange(100):
            # don't store data
            b[i] = i + 5
    
    pass
    # data store while leave context
    

# EDIT 1

ContextManager has **enter** , and **exit** method will

    
    
    class MemoryManager(object):
        def __init__(self):
            self.cache = {}
            self.buffer = False
    
        def __enter__(self):
            self.buffer = True
    
        def __store(self):
            # store data
            pass
    
        def __setitem__(self, key, value):
            self.cache[key] = value
            if not self.buffer:
                self.__store()
    
        def __exit__(self, exc_type, exc_value, traceback):
            self.__store()
            self.buffer = False
    

Therefore

    
    
    m = MemoryManager()
    with m as b:
        # __enter__ got called, set the manager to buffer mode
        # data won't store
        for i in xrange(100):
            # don't store data
            m[i] = i + 5    # in with block, so __store won't be called
    
    pass
    # __exit__ got called, __store automatically and leave buffer mode
    m[0] = 10  # not in buffer mode, __store will be called 
    

