Easiest way to add a function to existing class
问题 I'm using the python's built-in shelve module to manage some simple dictionaries. The problem I'm having is I want to use with shelve.open(filename) as f: , but when I try it claims DbfilenameShelf has no attribute __exit__ . So, I'm guessing the easiest way to do this is to wrap it in another class and add an __exit__ function to that wrapper. I tried this: class Wrapper(shelve.DbfilenameShelf): def __exit__(self): self.close() def __init__(self, filename, writeback=False): shelve