I stumbled upon some Python code that implements a REST API, which acts upon (i.e. get/set) a class like this:
class Model(
def __init__(self, foo=1, bar=0, baz=0):
self.foo = foo
self.bar = bar
self.baz = baz
def get_api(self):
return self.__dict__.keys()
def set_parameters(self, parameters):
for p in parameters.keys():
if p in self.__dict__.keys():
self.__dict__[p] = parameters[p]
Basically the REST API is "built" from get_api()
, therefore it matches 1:1 with the class' instance attributes.
Is it considered "good practice" in the Python world? (it looks a bit hack-ish to me)
If not, what would be a better way to store a model in a class?