python 函数装饰器

python 函数装饰器,第1张

概述举个例子 def a_decorator(func): def wrapTheFunc(): print "before decorator" func() print "end decorator" return wrapTheFunc@a_decoratordef a_func_need_decora 举个例子
def a_decorator(func):    def wrapTheFunc():        print "before decorator"        func()        print "end decorator"    return wrapTheFunc@a_decoratordef a_func_need_decorator():    print "In a_func_need_decorator()"a_func_need_decorator()

输出

before decoratorIn a_func_need_decorator()end decorator
等价

不是很明白? 

@a_decorator 

def a_func_need_decorator():

等价于

a_func_need_decorator = a_decorator(a_func_need_decorator)

修改下代码

def a_decorator(func):    def wrapTheFunc():        print "before decorator"        func()        print "end decorator"    return wrapTheFuncdef a_func_need_decorator():    print "In a_func_need_decorator()"a_func_need_decorator = a_decorator(a_func_need_decorator)a_func_need_decorator()

结果是一致的

什么?函数还可以作为对象传输

是的,举例

def test(a):    print atest2 = testtest2("hello")

输出

hello  
场景

账号验证

日志

 

参考

https://www.runoob.com/w3cnote/python-func-decorators.html

总结

以上是内存溢出为你收集整理的python 函数装饰器全部内容,希望文章能够帮你解决python 函数装饰器所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1195662.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存