So, this is more like a philosophical question for someone who is trying to understand classes.
Most of the time, how I use classes are actually a very bad way to use them. I think of a lot of functions and after a time just indent the code and makes it a class, replacing stuff with self.variable if a variable is repeated a lot. (I know it’s bad practise)
But anyways… What I am asking is:
class FooBar:
def __init__(self,foo,bar):
self._foo = foo
self._bar = bar
self.ans = self.__execute()
def __execute(self):
return something(self._foo, self._bar)
Now there are many ways to do this:
class FooBar:
def __init__(self,foo):
self._foo = foo
def execute(self,bar):
return something(self._foo, bar)
Can you suggest which one is bad and which one is worse?
Or suggest any other ways to do this.
This is just a toy example of course. I mean, there is no need to have a class here if there is only one function.. but let’s say in __execute something() calls a whole set of other methods?
Thanks
If each
FooBaris responsible forbarthen the first is correct. Ifbaris only needed forexecute()but notFooBar‘s problem otherwise, the second is correct.