python - higher-order function for delegating property getter/setters? -
i have python object bunch of property methods following, forward on method:
class wonderland(object): ... @property def tweedledee(self): return self.request('gettweedledee') @tweedledee.setter def tweedledee(self, value): result = self.request('settweedledee', value) if not result['success']: raise wonderlandissue(result['issue']) @property def tweedledum(self): return self.request('gettweedledum') @tweedledum.setter def tweedledum(self, value): result = self.request('settweedledum',value) if not result['success']: raise wonderlandissue(result['issue']) @property def mad_hatter(self): return self.request('gethatter') @mad_hatter.setter def mad_hatter(self, value): result = self.request('sethatter',value) if not result['success']: raise wonderlandissue(result['issue'])
is there way simplify using higher-order decorator write can this:
def setup_request_property(methodname, requestname): gettertopic = 'get'+requestname settertopic = 'set'+requestname def getter(self): return self.request(gettertopic) def setter(self, value): result = self.request(settertopic, value) if not result['success']: raise wonderlandissue(result['issue']) # hmm, how attach getter , setter object? class wonderland(object): ... tweedledee = setup_request_property('tweedledee','tweedledee') tweedledum = setup_request_property('tweedledum','tweedledum') mad_hatter = setup_request_property('mad_hatter','hatter')
?
Comments
Post a Comment