1

Is it proper practice to have all code within classes? I have one class that does all my calculating and whatnot. But I have all the rest of the code (mainly used to call the class) outside of a class. It looks like this.

class bigClass:
  executing here
  functions and whatnot
  blah blah

b=bigClass()
b.bigClassfunction()

My question is whether those last two lines should go in a class of their own? Or do I just leave them to float about not bound to a class.

user1675111
  • 8,695
  • 4
  • 18
  • 14
  • I think it probably depends on the context, if the functions are only relevant to the class they should be in the class (it can be a @staticmethod or @classmethod). – Andy Hayden Sep 24 '12 at 13:37

3 Answers3

1

That's absolutely OK, there's no need to put them in a class. A function could be an option if you need to repeat the code several times.

A class shouldn't be used for things like this; The role of class, as in Wikipedia, is

In object-oriented programming, a class is a construct that is used to create instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members which enable its instances to have state and behavior. Data field members (member variables or instance variables) enable a class instance to maintain state. Other kinds of members, especially methods, enable the behavior of class instances. Classes define the type of their instances.

Although you can embed this code in a class, it would be unnecessary to put this inside a class if it needs to be executed only once.

EDIT: As I now understand, the confusion is about how to indicate python which code to run first, like you would do in java using a main method in the ProjectName class. In python, the code runs top-down. Each statement is being calculated on the go. That's why you cannot reference to a class above its definition, for example.

obj = Klass()
class Klass: pass #Doesn't work!
unddoch
  • 5,790
  • 1
  • 24
  • 37
0

your question is not especially clear but you would always put all code related to a class within the class. It makes no design sense to do other wise.

cobie
  • 7,023
  • 11
  • 38
  • 60
  • It's not related to a class. It's just normal code that comes outside of any class. I was just wondering if that code needs its own class and that class was special in such a way that it would execute before all other classes (sort of like a main class in Java). Not necessary, just wondering if it was practice. – user1675111 Sep 24 '12 at 13:41
  • `main` in java is a method, not a class! – unddoch Sep 24 '12 at 13:42
  • 4
    python is not java. no need for classes when they do not support your design. – ch3ka Sep 24 '12 at 13:43
0

Some people put their "main" code into a block such as:

if __name__ == '__main__':
    foo()
    bar()

See this thread for more information.

Do not use classes for the sake of having classes, however. It isn't very "Pythonic".

Community
  • 1
  • 1
vinnydiehl
  • 1,654
  • 1
  • 13
  • 17