On this page
shelve.Shelf
Prev: shelve.open | Up: shelve
Rel: shelve.BsdDbShelf, shelve.DbfilenameShelf, shelve.open
Ref: docs.python.org — shelve.Shelf
Tags: #public #python3 #stdlib #shelve
Import
import shelve
from shelve import Shelf
Signature & Overview
shelve.Shelf(dict, protocol=None, writeback=False, keyencoding='utf-8')
Base class for shelf implementations.
Methods & Attributes
Shelf.clear(self)— D.clear() -> None. Remove all items from D.Shelf.close(self)—Shelf.closemethod/property.Shelf.get(self, key, default=None)—Shelf.getmethod/property.Shelf.items(self)— D.items() -> a set-like object providing a view on D's itemsShelf.keys(self)— D.keys() -> a set-like object providing a view on D's keysShelf.pop(self, key, default=<object object at 0x1040b41c0>)— value. If key is not found, d is returned if given, otherwise KeyError is raised.Shelf.popitem(self)— as a 2-tuple; but raise KeyError if D is empty.Shelf.setdefault(self, key, default=None)— D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in DShelf.sync(self)—Shelf.syncmethod/property.Shelf.update(self, other=(), /, **kwds)— If E present and has a .keys() method, does: for k in E.keys(): D[k] = E[k] If E present and lacks .keys() method, does:Shelf.values(self)— D.values() -> an object providing a view on D's values
Usage
import shelve
# Wrap any bytes->bytes mapping with pickle serialization
backing: dict[bytes, bytes] = {}
with shelve.Shelf(backing) as s:
s['k'] = [1, 2, 3]
assert s['k'] == [1, 2, 3]
Gotchas
- In-place mutations to mutable values aren't saved unless reassigned (or
writeback=True).
REPL Inspect
from shelve import Shelf
help(Shelf)
Linked from
Related notes
- tuts/python3/stdlib_module/shelve/BsdDbShelf Linked here; links to this page; shared tags: #shelve, #stdlib
- tuts/python3/stdlib_module/shelve/DbfilenameShelf Linked here; links to this page; shared tags: #shelve, #stdlib
- tuts/python3/stdlib_module/shelve/open Linked here; links to this page; shared tags: #shelve, #stdlib
- tuts/python3/stdlib_module/shelve Linked here; links to this page; shared tags: #stdlib
- tuts/c/stdlib shared tags: #stdlib
- tuts/c/stdlib/assert shared tags: #stdlib
- tuts/c/stdlib/assert/assert shared tags: #stdlib
- tuts/c/stdlib/assert/static_assert shared tags: #stdlib
- tuts/c/stdlib/ctype shared tags: #stdlib
- tuts/c/stdlib/ctype/isalpha shared tags: #stdlib