Difference between commands to stop execution

5

I've noticed that there are several ways to stop a run.

Basically what is the difference between using the commands:

  • break;

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

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

  • exit ();

  • quit ();

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

    
asked by anonymous 24.10.2018 / 16:59

2 answers

4
  

break

Exit only from a loop and does not terminate the application, it is less than return . Up to return can terminate the application when it is in the same function as the entry point.

  

sys.exit() (From module sys );

Terminates the application by throwing an exception ( SystemExit ) that can be handled at some point and all process termination, including resource release is performed before completion. It is considered that its use is not ideal and should only be called in very specific cases where you fully understand what you are doing and need exactly this immediate termination solution. I personally do not think so bad so if you really do not have anything else to accomplish, at least in well-done architectures that have no dependence on a specific shutdown. Use it preferably.

  

os._exit() (From module os );

Close the execution immediately without giving you the chance to do anything else, not even the release of resources.

  

exit()

Same as sys.exit() , but it is not intended to be used. I'm 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. This is the language problem that starts out to be script and then changes to be more than this.

  

quit()

Only one nickname for the previous function. Clear violation of "just have a way to accomplish something".

There is nothing kernel there, just that os.exit() is a shutdown call that probably asks the OS to terminate without further ceremony. There is no link to any routine. This seems to be a misunderstanding. It's a function like any other you call at some point in the code and it does something, in case that something is closing the application.

    
24.10.2018 / 17:33
0

Actually, there are only 3 ways to end the application:

  • Usually , that is, the code runs to the end of the main script;
  • An exception that is not handled with try / except , causing an error message ( traceback );
  • os.exit_() .
  • The other methods you passed do not end the program definitively - they only return from functions ( return ), exit loops ( break ) or go up an exception SystemExit methods). The program will only terminate if this causes the code to fall into one of the three situations mentioned above.

    The SystemExit exception does the same as any other exception: The functions and scopes are terminated, and the code flow returns in the call stack until the exception is handled with try / except or return to the main scope, where, if untreated, will cause the program to end. The only difference is that a traceback message does not print when the exception is SystemExit .

        
    24.10.2018 / 18:18