示例代码:
def setfunc(func):
def callfunc(*args, **kwargs):
print("-------")
print("*******")
ret = func(*args, **kwargs) #
# func(args, kwargs) 这种方法不行,它相当于传递了两个参数:一个元祖,一个字典
return ret
return callfunc
# 装饰一个有返回值的函数
@setfunc
def test1(num, *args, **kwargs):
num = num
args = args
kwargs = kwargs
return "num is {}, args are {}, kwargs are {}".format(num, args, kwargs)
ret = test1(10, 200, 300, mm=100)
print(ret)
# 返回元组
@setfunc
def test2(num, age):
return num, age
ret = test2(12, 3)
print(ret)
# 装饰一个没有返回值的函数
@setfunc
def test3(num1, num2):
print(num1, num2)
ret = test3(1, 2)
print(ret)
运行结果: