I have a Python class that has several "major" methods, which successively modify one of its attributes, e.g.
def method1(self):
# modify an attribute of self
return self
def method2(self):
# modify the same attribute of self
return self
Each of these core methods, in turn, calls multiple "helper" class methods that also modify the same attribute, i.e.
def method1(self):
self = self.helper_method1().helper_method2()
return self
def method2(self):
self = self.helper_method3().helper_method4()
return self
Is there a consensus on where (on what level) these "helper" methods should be defined inside a class?
I.e. is this:
def helper_method1_to_be_called_by_method1(self):
# modify at attribute of self
return self
def helper_method2_to_be_called_by_method1(self):
# modify at attribute of self
return self
def method1(self):
self = self.helper_method1().helper_method2()
return self
preferable to this (or vice versa):
def method1(self):
def helper_method1_to_be_called_by_method1(self):
# modify at attribute of self
return self
def helper_method2_to_be_called_by_method1(self):
# modify at attribute of self
return self
self = self.helper_method1().helper_method2()
return self
Or is there a third strategy that works best in terms of performance, ease of readability, and maintenance?
The thing is that you never know how flexible your design should be. One extreme case will be to nest each and every related helper method (your approach #2). Another edge case is to put each helper method in a separate file (module) and call it something like "method1_helper_utils.py". Unless you know the exact software design in advance, I'd suggest to do it this way: