https://www.liujiangblog.com/course/python/39
import logging
# vesion1, add try...catch... for all method.
def outer(func):
try:
result = func()
print("result is %s" % result)
return result
except Exception as ex:
print("failed")
raise Exception("failed due to %s ", str(ex))
@outer
def f1():
print("f1")
return None
# vesion2, decoratoer with specific parameter
def outerWithParam(func):
def inner(name):
try:
result = func(name)
print("result is %s" % result)
return result
except Exception as ex:
print("failed")
raise Exception("failed due to %s ", str(ex))
return inner
@outerWithParam
def f2(name):
# a = 1/0
print("f2 %s" % name)
return None
f2("kake")
# version 3, decorator with **kwargs.
def outerWithKwargs(func):
def inner(**kwargs):
try:
result = func(**kwargs)
print("result is %s" % result)
return result
except Exception as ex:
print("failed")
raise Exception("failed due to %s ", str(ex))
return inner
@outerWithKwargs
def f3(name):
print("f2 %s" % name)
return None
f3(name='aa')