class - Python: method with variable name for setting value of an object -
Values and ways to set them should be a simple class:
class Object: Def __init __ (self, id): self .__ id = id self__ value 1 = none at all self___ value2 = none ... etc def set_value1 (self, value1): self .__ value1 = value1 def Set_value2 (self, value2): self __value2 = value2 ... etc Can I merge them some way .set_valueX (valueX) - works to be one, it How is it done and easily imported without libraries?
This can be done using setter:
class Obj (object): def __setter (self, ** kwargs): key, value = kwargs.items () [0] setattr (self, "__" + key, value) def __init __ (self, id): self. Id = id self .__ value1 = none .__ value2 = none # You should assign the setter to the set function as follows: self.set_value1 = self .__setter self.set_value2 = self .__setter #and On this ... init method can be reduced, we use the same technique
Comments
Post a Comment