In Python, I want to create a new object by loading a number of variables into it. The easiest way is to pass a dictionary, but that makes programming very annoying: instead of self.health I have to call self.params['health'] all the time. Is there any way to set variable names (fields) dynamically?
I have:
DEFAULT_PARAMS = {
'health': 10,
'position': []
}
def __init__(self, params = DEFAULT_PARAMS):
self.params = params
print self.params['health']
I want to have:
DEFAULT_PARAMS = {
'health': 10,
'position': []
}
class Name():
def load(self, params):
# what goes here?
def __init__(self, params = DEFAULT_PARAMS):
self.load(params)
print self.health