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
帳號驗證
日誌