I have a class where I want to initialize the self.listN and add_to_listN for each list item, for example. from attrs = ['list1', 'list2'] I want list1 and list2 initialized as empty lists and the add_to_list1 and add_to_list2 methods that must be created. Each add_to_listN method must take two parameters, for example value and unit , and add a tuple (value, unit) to the corresponding listN .
At the end, the class should look like this:
class Foo(): def __init__(self): self.list1 = [] self.list1 = [] def add_to_list1(value, unit): self.list1.append((value, unit)) def add_to_list2(value, unit): self.list2.append((value, unit))
Leaving aside all the checks and the rest of the class, I came up with the following:
class Foo(): def __init__(self): for attr in ['list1', 'list2']: setattr(self, attr, []) setattr(self, 'add_to_%s' % attr, self._simple_add(attr)) def _simple_add(self, attr): def method(value, unit=None): getattr(self, attr).append((value, unit)) return method
I also tested other solutions, such as those suggested here , and I would like to do it “correctly”, so my questions are:
- Are / should these methods (be) actually a
classmethod or not? - Are there any costs for creating methods in
__init__ , in which case is there an alternative? - Where is the best place to run the
for loop and add these methods? Within the class definition? From this? - Is the use of metaclasses in this case?
Update
Although Benjamin Hodgson makes some good points , I am not asking for a (possibly better) alternative way to do this, but for the better use of these tools I mentioned. I use a simplified example so as not to focus on the details.
To clarify my questions: add_to_listN methods should be optional, and not replace setters / getters (so I still want to be able to do l1 = f.list1 and f.list1 = [] with f = Foo() ).