38

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?

6
  • 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. Commented Oct 24, 2011 at 3:21
  • Oh, there are many more methods in this class. I just didn't include them here. Commented Oct 24, 2011 at 3: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. Commented Oct 24, 2011 at 4: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? Commented Oct 24, 2011 at 4: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. Commented Oct 24, 2011 at 5:26

3 Answers 3

69

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!"
Sign up to request clarification or add additional context in comments.

4 Comments

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?
@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.
Ok, thank you. I think I need classes to show hierarchy and inheritance structures though, right?
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.)
15

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.

Comments

2

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.

1 Comment

Do you know why? That seems like a fairly significant deficiency, although I've not yet had a use case for it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.