def suppress(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception:
pass
return wrapper
def myfunc():
print("foo")
print("foo")
I found this code in a book, and ran it how it said...
suppress(myfunc)
The book said it was supposed to run the function but suppress the error in it, which was in print("foo")
Instead, it just gave me...
<function myfunc at 0x6981e0>
Why???
suppressdoes: it takes one function and gives you another function. If you want to call that function, go right ahead, call it:suppress(myfunc)()(notice extra parentheses at end of this to call the function.