In order to avoid the overhead associated with the shelve module's writeback
option I'm interested in putting together a shelf class that only accepts hashable values, with hashability being a proxy for immutability. So I'd like to subclass shelve.Shelf
and override the __set__
method. The catch is that shelve.open
can return one of a number of different classes (e.g., Dbfilenameshelf
), and I'd like my code to allow shelve
this flexibility.
Ideally a solution would have the following properties:
- No need to wrap every function that
Shelf
provides, as in:def keys(self): return self._shelf.keys()
- Will not break if Python adds new methods to
Shelf
or new subclasses ofShelf
- Avoiding fragility, by which I mean doing something complicated that could easily trip up someone (conceivably me) who is modifying or using the code months or years down the road. An example in this context would be reassigning
Shelf.__set__
, as inShelf.__set__ = my_func
. To me defining a new__getattr__
as @Winston hesitantly offers seems moderately fragile--I take it that's why he suggests it only with hesitation
Less important but still desirable would be:
- Consistent with object-oriented design principles. It seems to me that the new class here is a shelf, not that it has a shelf. And probably not both (it's not a shelf which contains another shelf)