By kmiklas


2019-07-11 19:08:49 8 Comments

In C++, the main() function is standardized to be of type int, and thus should return(0) with successful execution. Also, programs will typically print one line feed at the end to flush stdout, like so:

MINIMAL, COMPLETE, VERIFIABLE EXAMPLE

hello_world.cpp

#include <iostream>
int main() {
    std::cout << "hello, world" << std::endl;

    printf("\n");
    return(0);
}

hello, world

Is there an equivalent way to properly exit a Python script? Does it make sense to also print a line feed, and then explicitly exit with code 0?

hello_world.py

import sys
print("hello, world")

print('\n')
sys.exit(0)

hello, world

Tyvm :^)

1 comments

@Green Cloak Guy 2019-07-11 19:16:28

A python script ends when there's no more code to run. The textbook hello world program is one line:

print("hello, world!")

Python's print() function is equivalent to other languages' println() function, in that it prints a newline by default. You can change this behavior with the optional end keyword argument:

print("Hello", end=" ")
print("World!")

And if you just want to print an extra line for whatever reason, you don't even have to go to the extra trouble of specifying it - you can just call print() with no arguments and it'll print a blank line.

Most python scripts you'll see will additionally have a feature like this:

if __name__ == "__main__":
    print("hello, world!")

This is essentially a filter to make sure that the current code is being run as a script, and that it wasn't just imported by some other file (__name__ is a built-in variable that states the name by which the module was imported, or "__main__" if it was run directly from the command line and thus wasn't imported from anywhere).

By default, a python script will exit with the code 0. You can use the built-in exit() (or sys.exit() if you want to be a bit more safe - see @cdarke's excellent comment below) if you want to exit with a different return code (e.g. if your script encounters an error and you want to exit gracefully). Otherwise, the standard practice is to just let the script end on its own.

@kmiklas 2019-07-11 19:23:15

So... how to properly finish a Python script? Note that I'm intentionally avoiding the words terminate, exit, and the like.

@cdarke 2019-07-11 19:24:10

@kmiklas: with a blank line

@kmiklas 2019-07-11 19:24:34

Whoa... is this truth? I feel strangely unfulfilled by this answer.

@Error - Syntactical Remorse 2019-07-11 19:26:59

@kmiklas PEP states there should be one blank line at the end of your file. That line is not needed but that is common formatting. Python doesn't require anything fancy to say "I am now done with my code", just stop typing :)

@cdarke 2019-07-11 19:34:05

You can use the built-in exit() function - no. There appears to be a built-in called exit which has the same behaviour as sys.exit, but appearances can be deceptive. exit is not a built-in function but a site.Quitter callable object which raises a SystemExit exception. The site module is usually automatically loaded on start-up, but can be supressed with the -S command-line option, in which case exit will not work.

@Green Cloak Guy 2019-07-11 19:36:15

Thanks for pointing that out, @cdarke. I've edited my answer accordingly

@cdarke 2019-07-11 19:39:22

See also docs.python.org/3/library/constants.html#exit, specifically "They are useful for the interactive interpreter shell and should not be used in programs."

Related Questions

Sponsored Content

37 Answered Questions

[SOLVED] How do I check whether a file exists without exceptions?

22 Answered Questions

[SOLVED] How do I list all files of a directory?

  • 2010-07-08 19:31:22
  • duhhunjonn
  • 3386132 View
  • 3474 Score
  • 22 Answer
  • Tags:   python directory

21 Answered Questions

41 Answered Questions

[SOLVED] How to merge two dictionaries in a single expression?

16 Answered Questions

[SOLVED] What are metaclasses in Python?

57 Answered Questions

[SOLVED] Calling an external command in Python

12 Answered Questions

[SOLVED] How can I make a time delay in Python?

10 Answered Questions

[SOLVED] Does Python have a string 'contains' substring method?

33 Answered Questions

[SOLVED] How to get the current time in Python

  • 2009-01-06 04:54:23
  • user46646
  • 2837545 View
  • 2457 Score
  • 33 Answer
  • Tags:   python datetime time

11 Answered Questions

[SOLVED] How to substring a string in Python?

  • 2009-03-19 17:29:41
  • Joan Venge
  • 2570084 View
  • 1908 Score
  • 11 Answer
  • Tags:   python string

Sponsored Content