Skip to content

Instantly share code, notes, and snippets.

@0xallie
Last active September 30, 2022 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0xallie/f2a5962995ed1adb403d7d27b3e7950e to your computer and use it in GitHub Desktop.
Save 0xallie/f2a5962995ed1adb403d7d27b3e7950e to your computer and use it in GitHub Desktop.
exit() vs sys.exit()
❯ 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