Best way to design a class in python

Posted by Fraz on Stack Overflow See other posts from Stack Overflow or by Fraz
Published on 2012-09-12T15:29:14Z Indexed on 2012/09/12 15:38 UTC
Read the original article Hit count: 142

Filed under:
|
|
|

So, this is more like a philosophical question for someone who is trying to understand classes.

Most of time, how i use class is actually a very bad way to use it. I think of a lot of functions and after a time just indent the code and makes it a class and replacing few stuff with self.variable if a variable is repeated a lot. (I know its 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 any other way to do this.

This is just a toy example (offcourse). I mean, there is no need to have a class here if there is one function.. but lets say in __execute something() calls a whole set of other methods.. ?? Thanks

© Stack Overflow or respective owner

Related posts about python

Related posts about oop