#python #datastructure #datamodel #programming #language
-
__missing__
deal withkeys
when its nos presented on thedict
-
Try to use Inheritance From Collections.UserDict instead of
dict
because-
It has already some methods implemented,
__get__
and__update__
-
it uses a
self.data
variable, which is adict
, to store values and key, avoiding recursion on__setitem__
method and simplify__contains__
.
-
It has already some methods implemented,
- Key must be hashable: Hashable
class StrKeyDict0(dict): # Using dict as superclass
def __missing__(self, key):
if isinstance(key, str):
raise KeyError(key)
return self[str(key)] # Transforming into string to avoid recursion
def get(self, key, default=None):
try:
return self[key]
except KeyError:
return default
def __contains__(self, key):
return key in self.keys() or str(key) in self.keys()
-
Now using
UserDict
for the difference:
class StrKeyDict(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 # self.data object storing key and values
def __setitem__(self, key, item):
self.data[str(key)] = item
References
Ramalho, 2022, p83-96