Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this mean exit (main())

Tags:

python

I have seen this in a couple of python script I have been reading lately. I have looked at the doc; they only give examples deal with passing a termination value or string of some sort.

I think what this does is call the main method, then exit?

If someone could shed some light on this I would appreciate it.

if __name__ == "__main__":    exit (main()) 

Please and thank you

like image 211
myusuf3 Avatar asked Mar 12 '11 01:03

myusuf3


People also ask

What does the exit () do?

The exit () function is used to break out of a loop. This function causes an immediate termination of the entire program done by the operation system.

What does the exit () do in Python?

exit() method is used to terminate the process with the specified status. We can use this method without flushing buffers or calling any cleanup handlers. After writing the above code (python os. exit() function), the output will appear as a “ 0 1 2 “.

What Does main () do in Python?

The main function in Python acts as the point of execution for any program. Defining the main function in Python programming is a necessity to start the execution of the program as it gets executed only when the program is run directly and not executed when imported as a module.

What is the difference between exit () and SYS exit ()?

exit is a helper for the interactive shell - sys. exit is intended for use in programs. The site module (which is imported automatically during startup, except if the -S command-line option is given) adds several constants to the built-in namespace (e.g. exit ).


2 Answers

This will call the function main() and when main finishes, it will exit giving the system the return code that is the result of main().

A simplified example where this might be used:

def main():     try:         doSomething()         return 0     except:         return 1   if __name__ == "__main__":     exit (main()) 


If an explicit return value is not given in main(), the default value of None will be returned. This produces the same system return code as explicitly specifying return 0. If main returns anything other than an integer or None a system return code of 1 will be produced.

like image 134
amicitas Avatar answered Oct 08 '22 04:10

amicitas


If you execute a Python script directly, __name__ is set to "__main__", but if you import it from another script, it is not.

So in this case, the script is seeing if you're executing it directly. If it is, it calls the main() function to perform some work, and returns the return value of the main() function to the system via exit(). If the script is being imported from another module, it doesn't execute the main() function and simply provides the script's functions and classes to the importing script.

This is a common idiom in Python. It allows you to have scripts that are standalone programs, but can also be imported without trying to do work that the importing script doesn't want done.

like image 45
Chris Lutz Avatar answered Oct 08 '22 04:10

Chris Lutz