Skip to content

Instantly share code, notes, and snippets.

@seahrh
Created June 4, 2021 06:23
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 seahrh/7dee6d3f083df1f001048baba317183e to your computer and use it in GitHub Desktop.
Save seahrh/7dee6d3f083df1f001048baba317183e to your computer and use it in GitHub Desktop.
@functools.wraps(f) makes decorator pickleable; works with multithreading
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