def func(funcname):
def func_in(*args,**kwargs):
ret = funcname(*args,**kwargs)
return ret
return func_in
@func
def test():
print("---test---")
return "haha"
@func
def test2():
print("---test2---")
@func
def test3(a):
print("---test3---a=%d--"%a)
ret = test()
print("test retuen value is %s"%ret)
a = test2()
print("test2 retuen value is %s"%a)
test3(11)
python 通用装饰器
最新推荐文章于 2025-03-18 16:16:53 发布
博客展示了Python中函数装饰器的代码实现。定义了装饰器函数func,它接收一个函数作为参数,内部定义了包装函数func_in来调用原函数并返回结果。还给出了使用该装饰器的多个函数示例,如test、test2和test3。
754

被折叠的 条评论
为什么被折叠?



