How to implement a persistent Python `list`?
<p>I'm trying to make an object act like a built-in <code>list</code>, except that its value be saved once modified.</p>

<p>The implementation I come up with is wrapping a <code>list</code> in a <code>PersistentList</code> class. For every access to method that may change the list, the wrapper delegates to the wrapped <code>list</code>, and save it to a key-value database after it's invoked.</p>

<p>Code:</p>

<pre><code>class PersistentList(object):
    def __init__(self, key):
        self.key = key 
        self._list = db.get(key, []) 

    def __getattr__(self, name):
        attr = getattr(self._list, name)
        if attr:
            if attr in ('append', 'extend', 'insert', 'pop',
                'remove', 'reverse', 'sort'):
                attr = self._autosave(attr)
            return attr
        raise AttributeError

    def _autosave(self, func):
        @wraps(func)
        def _(*args, **kwargs):
            ret = func(*args, **kwargs)
            self._save()
            return ret 
        return _

    def _save(self):
        db.set(self.key, self._list)
</code></pre>

<p>There are several problems with this implementation:</p>

<ol>
<li><p>I have to decorate methods like <code>append</code> every time they are
accessed, is there a better way to decorate multiple methods of some
object?</p></li>
<li><p>Operations like <code>l += [1,2,3]</code> don't work because I haven't
implemented the <strong>iadd</strong> method.</p></li>
</ol>

<p>What can I do to simplify this?</p>
