Python: what is the difference between explicit output and just allowing execution to the end of the file?

For a simple python script that does not start any threads or fork of any processes, what is the difference between the simplicity of executing until the end of the script and the explicit call to quit (), exit () or sys.exit ()?

i.e. What's the difference between

def main():
    # do some stuff
    print("Done doing stuff")

if __name__ == '__main__':
    main()

and

import sys

def main():
    # do some stuff
    print("Done doing stuff")
    # explicit exit
    sys.exit()

if __name__ == '__main__':
    main()

I ask about this because I had some problems with garbage collection at the end of the script (exceptions raised from __del__, apparently, due to the order in which things are cleared) without an explicit call exit()and adding an explicit exit()one seems to fix these problems . Or at least none of these exceptions will be printed after the call exit(), perhaps they are simply disabled.

+4
1

, , . , , .

:

try:
    ...
except KeyboardInterrupt:
    x = input('Sure you want to exit? y/n')
    if x == 'y':
        quit()
    else:
        pass
0

Source: https://habr.com/ru/post/1683440/


All Articles