Hello,
I am looking for good techniques to split up your thousands of methods that your class can have.
- Inheritance is interesting to put core methods in your super class.
- Compositing seems interesting to but with this you will have to redefine each method in the class and then launch the method of the ‘leaf’.
(main resource : Learn Python the Hard Way)
I was wondering in how far following ‘pattern’ setup is a good idea :
Group all methods by category and put them in a seperate class that is not hooked.
In the init of this class you pass the main class so that your methods can use this (instead of using self)
Example:
I have a very complex object called Job
job does a lot of different this as creating processes, constructing filepaths, etc.
If we focus on a constructing filepaths we have a ‘construct_output_filepath()’ method.
with composting and inheritance you will get:
job.construct_output_filepath()
but the idea I had is to do following
job.path.construct_output_filepath()
This way you keep all methods considering path construction encapsulated in a ‘path’ variable.
To get this we would have following code structure :
class JobPath(object):
def init(self, job):
self.job = job
def construct_output_filepath(self):
return self.job.name + ‘.exr’
class Job(object):
def init(self):
self.path = JobPath(self)
In how far is this a good idea ?
Because the JobPath will know and depend on Job and Job will depend on JobPath.
This make both classes very hooked to eachother and possibly fragile ?
Are there other good techniques to use or idea’s ?
Maybe is this kind of structure ok, is there a pattern that defines this behaviour ?
Some more idea’s about this would be great !
Thanks in advance!
Sven