python - updating values of class attributes -
i have class following:
class myclass(object): def __init__(self, input1, input2): self.attribute1 = [(a1_d1, a1_p1), (a1_d2, a1_p2)] self.attribute2 = [(a2_d1, a2_p1), (a2_d2, a2_p2), ..., (a2_d10, a2_p10)] ...some other attributes here...
the first coordinate in every pair decision/action , second coordinate probability action chosen. want write function updates these probabilities instance of class program runs. way probabilities updated depends on decision taken previously. example, can write functions of following sort:
def update_probabilities_attribute1(self, decision): = 0 action, current_probability in self.attribute1: code here self.attribute1[i] = (action, new_probability) = + 1 def update_probabilities_attribute2(self, decision): = 0 action, current_probability in self.attribute2: code here self.attribute1[i] = (action, new_probability) = + 1
the part code here common 2 functions. there anyway can have 1 function instead of 2 different ones, takes self.attribute1 or self.attribute2 input , updates accordingly.
thanks.
you use getattr
, relatives:
def update_probabilities(self, attribute_name, decision): = 0 attr_value = getattr(self, attribute_name) action, current_probability in attr_value: #some code here attr_value[i] = (action, new_probability) = + 1 setattr(self, attribute_name, attr_value)
Comments
Post a Comment