python 函数装饰器

举个例子

def a_decorator(func):
    def wrapTheFunc():
        print "before decorator"
        func()
        print "end decorator"

    return wrapTheFunc


@a_decorator
def a_func_need_decorator():
    print "In a_func_need_decorator()"


a_func_need_decorator()

输出html

before decorator
In a_func_need_decorator()
end decorator

等价

不是很明白? python

@a_decorator bash

def a_func_need_decorator():函数

等价于spa

a_func_need_decorator = a_decorator(a_func_need_decorator)日志

修改下代码code

def a_decorator(func):
    def wrapTheFunc():
        print "before decorator"
        func()
        print "end decorator"

    return wrapTheFunc


def a_func_need_decorator():
    print "In a_func_need_decorator()"


a_func_need_decorator = a_decorator(a_func_need_decorator)
a_func_need_decorator()

结果是一致的htm

什么?函数还能够做为对象传输

是的,举例对象

def test(a):
    print a


test2 = test
test2("hello")

输出blog

hello  

场景

帐号验证

日志

 

参考

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

相关文章
相关标签/搜索