It's simple enough to do, though it would help a lot to know why you are trying to do it. The most Pythonic approach would possibly be to have a metaclass that creates all your accessor functions. If you were trying to do this right, I would look up the python property
paradigm. However, if all of your accessor functions are identical, why do you need separate functions for them? You can easily just set values via:
x = Object()
x.value = value
... So long as you dispense with the entirely unnecessary double-underscore, at least. If you really need to alter every call to every attribute, you can override __setattr__
(Python Data Model Reference). As a side note, in every group I've worked with, the __variable approach has been highly discouraged. It doesn't stop anyone from actually accessing the value (which you can do via instance._Object__value1
), but it makes subclassing horrible down the line.
If you want to just do the same pre-processing for every property get or set, you could always do something like:
class Object:
_ATTR_NAMES = ('value1', 'value2', ...)
def __init__(self):
for name in self._ATTR_NAMES:
setattr(self, "_" + name, None)
def set_attr(self, name, value):
if name in self._ATTR_NAMES:
setattr(self, name[1:], value)
else:
raise AttributeError("No attribute named %s found"%name)
Python's setattr is a function that sets a property with a given name on the attribute. In this case, you can simply have a list of properties, set them based on that, and retrieve them based on that using the corresponding 'getattr' function. Again though, I am wary of your reasons for doing this. Typically explicit is better than implicit. It's seldom hard to just make an extra get/set function that makes referencing much more clear.
If you truly must, you can also generate get/set functions using techniques very similar to the ones I just showed for properties (i.e., create functions on the fly that wrap set_attr
), but again, it usually makes code much harder to maintain and read. I would avoid many of these techniques unless you really have a strong and clear need to do so. If you really have to do it, something like this would work:
class Object:
_ATTR_NAMES = ('value1', 'value2', ...)
def __init__(self):
for name in self._ATTR_NAMES:
setattr(self, "_" + name, None)
def _set_attr(self, name, value):
if name in self._ATTR_NAMES:
setattr(self, name[1:], value)
else:
raise AttributeError("No attribute named %s found"%name)
# This binds named set functions to the class
for name in Object._ATTR_NAMES:
def tempSetter(self, value):
self._set_attr(name, value)
setattr('set_'+name, tempSetter)
del name
del tempSetter
This is a hacky way of doing it. It is more elegant to do this sort of processing in a metaclass. However, as it takes a lot of background to understand, I'm going to skip that. If you need to do pre-processing before each set function, it is generally much preferred to do something along the lines of:
def _preprocessing(self, value):
# Do something here
return value
def set_value1(self, value):
value = self._preprocessing(value)
self._value1 = value
def set_value2(self, value):
value = self._preprocessing(value)
self._value2 = value
Finally, if all you are worried about is the time to generate boilerplate get/set functions, then just write a script that writes the skeleton of your Python class as a text file. I have these types of scripts that outline my unit test cases based on the original modules. Or have a script macro in your text editor. Code is read many more times than it is written, so don't make complex code just to save a few keystrokes.