class PersistentList(list):

    def __init__(self, key, db):
        self.key = key
self.extend(db.get(key, []))

def _save(self):
    print('saving {x}'.format(x=self))

def __enter__(self):
    return self

def __exit__(self, ext_type, exc_value, traceback):
    self._save()
db = {}
p = PersistentList('key', db)
with p:
    p.append(1)
p.append(2)
with p:
    p.pop()
p += [1, 2, 3]
How to implement a persistent Python `list`?