Difference between commands to stop execution

Asked

Viewed 2,508 times

7

I found that there are several ways to interrupt an execution.

Basically what is the difference between using the commands:

  • break;

  • sys.Exit() ( From the module sys);

  • os. _Exit() ( From the module the);

  • Exit();

  • quit();

I noticed that some are linked to running a routine (sys.exit() as an example), and others to kernel (exit() among others ).

2 answers

6


break

Just comes out of a loop and does not finish the application, it is less than a return. Up to a return can close the application when it is in the same function as the input point.

sys.exit() ( Module sys);

Closes the application by launching an exception (SystemExit) which can be handled at some point, and any closure process, including release of appeals, is executed before it ends. It is considered that its use is not ideal and should only be called in very specific cases where you understand well what you are doing and need exactly this immediate termination solution. Particularly I don’t think so bad if it really has nothing else to accomplish, at least on well-made architectures that have no dependency on a specific closure. Use it preferably.

os._exit() (Module os);

Terminates the execution immediately without giving chance to perform anything else, not even the release of resources.

exit()

The same as the sys.exit(), but it is considered that it should not be used. I am not sure why, but I believe that functions that do not belong to modules become ambiguous. Use in many simple codes or in REPL when importing a module can be worse. That’s the problem of the language that starts to be script and then it changes to be more than that.

quit()

Just a nickname for the previous function. Clear violation of "only having a way to accomplish something".

There is nothing kernel there, only that the os.exit() is a closing call that probably asks the OS to end without further ceremony. There is no link to any routine. That seems like a misunderstanding. It’s a function like any other that you call at some point in the code and it executes something, in which case this something is the closure of the application.

  • 2

    Obs.: the functions exit() and quit() are defined in the module site, which is automatically imported into context by the interpreter, unless the flag is used -S when executing the code. With the flag, the functions will not be defined, so it is not very indicated to depend on them.

  • great answer, thank you! In my case for example, the ideal then is to use (sys.Exit()), because I intend to use in situations where it is not necessary to execute anything later. Really many options end up confusing rs

1

In fact, there are only 3 ways to finish the application:

  1. Normally, i.e., the code run until the end of the main script;
  2. Occur a exception which is not treated with try/except, causing an error message (traceback);
  3. os.exit_().

The other methods you have passed do not terminate the program definitively - they only return from functions (return), come out of loops (break) or go up a exception SystemExit (other methods). The program will only end if this causes the code to fall in one of the 3 situations mentioned above.

The exception SystemExit does the same as any other exception: Functions and scopes are terminated and the code flow goes back into the call stack until the exception is dealt with try/except or go back to the main scope, where if it has not been processed by then it will cause the program to end. The only difference is that a message like traceback is not printed when the exception is SystemExit.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.