Python 装饰器

__author__ = 'Brook Zhao'
import time
def login(cus_type):
    def runfunc(func):
        def kenner(*args,**kwargs):
            variable01=time.time();
            print("Customer Type:%s"%cus_type)  #对传入的用户类型进行处理
            result=func(*args,**kwargs)     #被装饰函数实体运行
            variable02=time.time();
            return result   #返回被装饰函数的返回值
        return kenner   #返回函数地址
    return runfunc      #返回函数地址

@login("qq")
def test001(a,b,c):
    print("in the test001 case")
    time.sleep(1)
    return (a+b+c)/3

@login("weixin")
def test002():
    print("in the test002 case,the case no return value")
    time.sleep(1)

result=test001(1,2,3)
print(result)
test002()

*************RESULT***************

Customer Type:qq
in the test001 case
2.0
Customer Type:weixin
in the test002 case,the case no return value

posted @ 2018-10-12 12:14  Happy_Bing  阅读(89)  评论(0)    收藏  举报