December 2018
Beginner to intermediate
796 pages
19h 54m
English
A shelf, is a persistent dictionary-like object. The beauty of it is that the values you save into a shelf can be any object you can pickle, so you're not restricted like you would be if you were using a database. Albeit interesting and useful, the shelve module is used quite rarely in practice. Just for completeness, let's see a quick example of how it works:
# persistence/shelf.pyimport shelveclass Person: def __init__(self, name, id): self.name = name self.id = idwith shelve.open('shelf1.shelve') as db: db['obi1'] = Person('Obi-Wan', 123) db['ani'] = Person('Anakin', 456) db['a_list'] = [2, 3, 5] db['delete_me'] = 'we will have to delete this one...' print(list(db.keys())) # ['ani', 'a_list', 'delete_me', 'obi1'] ...Read now
Unlock full access