37

I haven't done much python - coming from a C/Java background - so excuse me for asking such a simple question. I am using Pydev in Eclipse to write this simple program, and all I want it to do is to execute my main function:

class Example():

if __name__ == '__main__':
    Example().main()        <----- What goes here?


    def main(self):     
        print "Hello World!

That is what I have now. I have also tried

self.main() 

and

main()

and

main(self)

none of which work. What am I missing?

franka
  • 1,867
  • 3
  • 17
  • 31
  • 3
    A class that you instantiate precisely once in order to run a single method of it, is not a class. *Especially* if the class needs no arguments to instantiate it. – Ben Oct 24 '11 at 03:21
  • Oh, there are many more methods in this class. I just didn't include them here. – franka Oct 24 '11 at 03:57
  • 1
    Does the class present an interface used by other parts of the program, or does it just have other methods used by itself in its implementation of `main`? If yes, that suggests to me that `main` doesn't belong in this class. If no, that suggests to me that you're not really using the class as a class. – Ben Oct 24 '11 at 04:30
  • Yes, the class describes the user interface of this program, so other parts are dependent on it. Starting up the UI is the first thing that happens in program execution, so that's why the main method is there. Would you put it into a separate module/class? – franka Oct 24 '11 at 04:56
  • 1
    I usually have a `main` function at module scope. It calls other code, or creates the initial objects. Nothing calls it, and it doesn't manipulate any "internal state" the way the methods of normal classes do. But maybe what you're doing is more appropriate for your use case; it's hard to say without seeing your code. Just don't blindly use Java's `main` method idiom in Python if it doesn't actually help you! I find `main` is usually much more like a procedure than it is like a method. – Ben Oct 24 '11 at 05:26
  • Good answer here: http://stackoverflow.com/questions/419163/what-does-if-name-main-do – torina Feb 20 '17 at 22:43

3 Answers3

66

Well, first, you need to actually define a function before you can run it (and it doesn't need to be called main). For instance:

class Example(object):
    def run(self):
        print "Hello, world!"

if __name__ == '__main__':
    Example().run()

You don't need to use a class, though - if all you want to do is run some code, just put it inside a function and call the function, or just put it in the if block:

def main():
    print "Hello, world!"

if __name__ == '__main__':
    main()

or

if __name__ == '__main__':
    print "Hello, world!"
Amber
  • 507,862
  • 82
  • 626
  • 550
  • 1
    Thanks! Well I think I need to use classes to organize all my code since this is part of a bigger project. I am not really sure though - classes aren't commonly used in Python though, are they? – franka Oct 24 '11 at 03:12
  • 3
    @vegansmarties Classes are used often enough in Python. But classes are a way of structuring your program that has lots of semantic baggage. If all you need is organisation and namespaces, then just use modules, IMHO. It has always vaguely irritated me that Java's insistence that "everything must be part of a class" extends to making things into classes that do not remotely resemble them. – Ben Oct 24 '11 at 04:36
  • 1
    Ok, thank you. I think I need classes to show hierarchy and inheritance structures though, right? – franka Oct 24 '11 at 04:53
  • Yes, but also consider whether or not you actually need the inheritance. (It's quite possible the answer is yes, which is fine - but it might also be no. Just keep that in mind.) – Amber Oct 24 '11 at 05:31
14

That entire block is misplaced.

class Example(object):
    def main(self):     
        print "Hello World!"

if __name__ == '__main__':
    Example().main()

But you really shouldn't be using a class just to run your main code.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

Remember, you are NOT allowed to do this.

class foo():
    def print_hello(self):
        print("Hello")       # This next line will produce an ERROR!
    self.print_hello()       # <---- it calls a class function, inside a class,
                             # but outside a class function. Not allowed.

You must call a class function from either outside the class, or from within a function in that class.