August 2018
Intermediate to advanced
366 pages
10h 14m
English
A few things need attention when working with shelve.
First of all, shelve doesn't track mutations. If you store a mutable object (such as dict or list) in shelf, any change you do to it won't be saved. Only changes to the root keys of shelf itself are tracked:
>>> with shelve.open('/tmp/shelf.db') as shelf:
... shelf['value'].value = 10
...
>>> with shelve.open('/tmp/shelf.db') as shelf:
... print(shelf['value'].value)
...
5
This just means that you need to reassign any value you want to mutate:
>>> with shelve.open('/tmp/shelf.db') as shelf:
... myvalue = shelf['value']
... myvalue.value = 10
... shelf['value'] = myvalue
...
>>> with shelve.open('/tmp/shelf.db') as shelf:
... print(shelf['value'].value)
...
10
shelve doesn't ...