tags:

views:

352

answers:

5

Hi,

What is Python's equivalent of "public static void main(String[] args) { ... }"? I remember having used it in the past and then forgot.

What I'm basically trying to remember is a function which I think included some underscores (__)...

thx

+12  A: 
#!/usr/bin/env python

import sys

def main(args):
    print args

if __name__ == '__main__':
    main(sys.argv)

edit: emulate a void return.

Santa
JUST beat me to it :)
Devoted
it's slightly confusing to also have a method named `main(args)` here, as there is nothing special about it that causes it to be invoked by the python runtime. It's the check for `__name__=='__main__'` that counts
matt b
That's not exactly a `void` return, is it? :)
Daniel DiPaolo
@Daniel DiPaolo: Java doesn't allow you to return a value during a normal exit, only if you use `System.exit(int)`. This is different from other languages, so if a language would return a value from its main method, that would instead be correct. (`public static void main(String[] args)` is the entry point for a Java program)
R. Bemrose
@R. Bemrose you missed what it looked like before the edit. He had `main` ending with a `return 0`
Daniel DiPaolo
+8  A: 
if __name__ == "__main__":
    ....do your thing
Ben Hoffstein
A: 

Oh!

if __name__ == '__main__':
Devoted
Not quite. You need leading and trailing double underscores, as these are "magic" names.
Santa
+1  A: 

The classic versions above are most commonly used, but for something more comprehensive, check out Guido van Rossum's blog post:

All Things Pythonic: Python main() functions (also view the comments for more)

Nick T
+2  A: 

This is a commonly-used idiom, but it is NOT equivalent to Java's public static void main(String args[]). All Python modules execute from top to bottom all statements at the module's scope. For most imported modules, these statements are usually limited to class and method definitions, which set up values in the module's namespace, but don't actually execute the functions' code. But if you put a statement at module scope like SPECIAL_CONSTANT = 42, then this will be run immediately when the module is imported or run standalone - no need for main() or __main__ or anything. Try putting a print statement at the top of a simple module. Whether you import that module or run it by itself from the command line, the print statement will always execute.

What is nice about this idiom is that it allows you to embed some simple tests or demo code right in with a library that is typically imported by some module written by your customer. Maybe something like this:

# special_super_duper_module.py

# this next statements will run, even though not in main() or set off
# with "__name__ ==" tests or any such thing
print "You are using special_super_duper_module.py, written by Felix the Cat"
SPECIAL_CONSTANT = 42

def super_duper_function1():
    pass

def super_duper_function2():
    pass

if __name__ == "__main__":
    print "You are running special_super_duper_module.py interactively"
    assert super_duper_function1() == None, "expected None, got non-None result"
    assert super_duper_function2() == None, "expected None, got non-None result"
    # doctests or unittests could also go here, or just a simple demo

When the module is imported by some code that wants to use your super duper functions, then the little banner at the top will print, but the code that is conditionalized by if __name__ etc.... won't get run. But if your user runs the module directly using the python command, then the embedded test or demo code will get run.

In contrast, public static void main(String args[]) is a special method signature that tells the Java VM what what method of what class to start with. Python just starts at the top of the named module and begins running from there. That's why you only need to write:

print "Hello, World!"

in a module all by itself to write your first Python program.

Paul McGuire
+1 for the comprehensive answer instead of a superficial Gadarene-upticker-attracting answer :-)
John Machin
@John - I learned a new word today. (I never saw a Gadarene, I never hope..., etc. etc.)The thing I *hate* about `public static void main(String[] args)` is that it is such an enigmatic barrier to first-time developers, and invariably, instructors' responses to those who ask are "don't worry about what it means, just type it in". I consider it a language design flaw that one must learn 1. main as an entry point, 2. public method protection, 3. static method definitions, 4. void to indicate a function with no return, and 5. an array declaration, just to get the program to print your name!
Paul McGuire