python 装饰器 二 (装饰带参数的函数)

#!/usr/bin/env python
#_*_coding=utf-8_*_
#Author: xieyixue
#mail: xyx_0410@163.com
#Created Time: 2015年07月01日 星期三 00时28分03秒
def deco(func):
    '''定义装饰器接受参数,参数为被装饰的函数
        '''
    def _deco(*arg, **kwarg):
        '''定义方法执行被装饰的函数,接受2个参数'''
        #执行前-->执行
        print("before")
        #执行中-->执行被装饰函数
        ret = func(*arg, **kwarg)
        #执行后-->执行
        print("after")
        #返回
        return ret
    #执行定义的方法
    return _deco
#加上装饰器
@deco
def func2(a):
    '''定义的函数一定要有return,不然装饰器会失败'''
    print a
    return '返回值'
#执行下函数
func2("xixi",)
'''
执行结果
before -------执行函数前
xixi   ------执行函数
haha   ------执行函数
after  -------执行函数后
'''
posted @ 2015-07-01 01:05  谢义学  阅读(359)  评论(0)    收藏  举报