Last active
September 30, 2022 18:00
-
-
Save 0xallie/f2a5962995ed1adb403d7d27b3e7950e to your computer and use it in GitHub Desktop.
exit() vs sys.exit()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
❯ python | |
Python 3.10.7 (main, Sep 7 2022, 12:19:18) [GCC 10.2.1 20210110] on linux | |
Type "help", "copyright", "credits" or "license" for more information. | |
>>> try: | |
... exit() | |
... except SystemExit: | |
... print("no exit for you") | |
... finally: | |
... print("cleanup") | |
... | |
no exit for you | |
cleanup | |
>>> import sys | |
>>> try: | |
... sys.exit() | |
... except SystemExit: | |
... print("no exit for you") | |
... finally: | |
... print("cleanup") | |
... | |
no exit for you | |
cleanup | |
>>> | |
❯ python -m timeit -n 1000000 $'try: exit()\nexcept SystemExit: pass' | |
1000000 loops, best of 5: 472 nsec per loop | |
❯ python -m timeit -n 1000000 -s 'import sys' $'try: sys.exit()\nexcept SystemExit: pass' | |
1000000 loops, best of 5: 209 nsec per loop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment