python class variable update based on dictionary
How can I update class variable from dictionary? I've came up with a dirty
hack, but I'm looking (if there is) for something more neat.
Let's say I want a class C which parameters will be set based on given
dictionary. As a result, c should have
class C:
def setVar(self, var):
for key in var.keys():
exec('self.{} = {}'.format(key, var[key]))
D = {'a':1, 'b':2, 'c':3}
c = C()
c.setVar(D)
# c.a = 1
# c.b = 2
# c.c = 3
No comments:
Post a Comment