This question is somewhat language agnostic-agnostic, but not completely, since OOP is different in, for example, JavaJava, which doesn't have first-class functions, than it is in PythonPython.
In
In other words, I feel less guilty for creating unnecessary classes in a language like Java, but I feel like there might be a better way in the less boilerplatey languages like Python.
My program needs to do a relatively complex operation a number of times.
That operation requires a lot of "bookkeeping", has to create and delete some temporary files, etc.
That's
That's why it also needs to call a lot of other "suboperations" - putting everything into one huge method isn't very nice/modular/readable, modular, readable, etc.
class Thing:
def __init__(self, var1, var2):
self.var1 = var1
self.var2 = var2
self.var3 = []
def the_public_method(self, param1, param2):
self.var4 = param1
self.var5 = param2
self.var6 = param1 + param2 * self.var1
self.__suboperation1()
self.__suboperation2()
self.__suboperation3()
def __suboperation1(self):
# doDo something with self.var1, self.var2, self.var6
# doDo something with the result and self.var3
# self.var7 = something
# ...
self.__suboperation4()
self.__suboperation5()
# ...
def suboperation2(self):
# usesUses self.var1 and self.var3
# ...
# etc.
The problem I see with this approach is that the state of this class makes sense only internally, and it can't do anything with its instances except call their only public method.
# makeMake a thing object
thing = Thing(1,2)
# callCall the only method you can call
thing.the_public_method(3,4)
# youYou don't need thing anymore
The problem I see with this is that I have to pass a lot of variables between functions.
Also Also, the functions would be closely related to each other, but they wouldn't be grouped together.