I wrote a python class that inherit the dict builtin type. I want to write a sort of container with additional check methods.
If I create an instance of that class, I got a RecursionError. I do not understand this error.
class Params(dict):
""" A convenient container of all paramters """
# list of authorized keywords with default values
keywords = {"k1": 1, "k2": 2, "k3": 3}
def __init__(self, *args, **kwargs):
# self.update(self.keywords)
for k, v in self.keywords.items():
self[k] = v
self.update(*args, **kwargs)
def update(self, *args, **kwargs):
for k, v in dict(*args, **kwargs).items():
self[k] = v
def __setitem__(self, name, value):
""" set a parameters following dict syntax """
name = name.lower()
if name in self.keywords:
self[name] = value
else:
raise KeyError("Keywords %s does not exist." % name)
def __getattr__(self, name):
""" get paramters as an attribute of the class """
return self.__getitem__(name)