Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import collections
- class CustomUserDict(collections.UserDict):
- def __missing__(self, key):
- if isinstance(key, str):
- raise KeyError(key)
- return self[str(key)]
- def __contains__(self, key):
- return str(key) in self.data
- def __setitem__(self, key, item):
- self.data[str(key)] = item
- if __name__ == '__main__':
- d = CustomUserDict({1:"one", 2:"two", 3:"three"})
- print(d[1])
- print(d['1'])
- print(d[4]) # raises KeyError
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement