Created
December 18, 2018 19:44
-
-
Save toby-p/0f4384d0707c68038d5c1114635c80ea to your computer and use it in GitHub Desktop.
Python decorator to suppress errors.
This file contains hidden or 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
# Global variable to determine whether or not to raise errors. | |
SUPPRESS_ERRORS = True | |
def suppress_errors(func): | |
"""Decorator function to suppress errors.""" | |
if not SUPPRESS_ERRORS: | |
return func | |
def suppressed(*args, **kwargs): | |
try: | |
return func(*args, **kwargs) | |
except: | |
return | |
return suppressed | |
# Test function: | |
@suppress_errors | |
def test(a, b): | |
return a*b | |
# Test execution of function. If `SUPPRESS_ERRORS` variable is set to True then | |
# nothing will happen; if set to False, an error is raised as normal. | |
test(2.5, "dspkfd") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment