Created
June 4, 2021 06:23
-
-
Save seahrh/7dee6d3f083df1f001048baba317183e to your computer and use it in GitHub Desktop.
@functools.wraps(f) makes decorator pickleable; works with multithreading
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
def safe_func(default_value=None): | |
def decorate(f): | |
@functools.wraps(f) | |
def wrapper(*args, **kwds): | |
try: | |
res = f(*args, **kwds) | |
except: | |
res = default_value | |
return res | |
return wrapper | |
return decorate | |
@safe_func(default_value=None) | |
def div(x,y): | |
return x / y |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment