I'd like to create a function that writes to an attribute in a Python class, but be able to decide whether the attribute should be written.
class Test(object):
def __init__(self):
self.a = None
self.b = None
def write_attrib(self,which_attrib,value):
# perform a check here
which_attrib = value
def write_a(self,value):
self.write_attrib(self.a, value)
def write_b(self,value):
self.write_attrib(self.b, value)
if __name__ == '__main__':
t = Test()
t.write_a(5)
t.write_b(4)
print(t.a, t.b)
Now this will result in both values being None
, so they haven't been touched by the function.
I'd like to do this approach so I can do some checks in write_attrib
before writing the attribute and I don't have to write these checks twice (but would like to make the convenience functions write_a
and write_b
available).