Add values to a class in Python -
this question has answer here:
- how programmatically set attribute? 3 answers
let's have following in python:
class test(): self.value1 = 1 self.value2 = 2 def setvalue1(self, value): self.value1 = value
so 1 can set value1 doing:
test.setvalue1('hola')
or
test.value1 = 'hola'
so far good. problem set values reading them somewhere else instance have following:
a = [['value1','hola'],['value2','adios']]
i able run (in pseudo code):
for each in a: test.each[0] = a[1]
is possible? much!
you that:
class test: def __init__(self): self.val1 = 1 t = test() setattr(t, 'val2', 2) print t.val2
or that:
class test: def __init__(self): self.val1 = 1 def setself(self, name, val): self.__dict__[name] = val t = test() t.setself('val3', 3) print t.val3
Comments
Post a Comment