首页 > 编程语言 > 详细

Python 装饰器

时间:2018-10-12 13:17:38      阅读:177      评论:0      收藏:0      [点我收藏+]

__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()

#****************************

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

Python 装饰器

原文:https://www.cnblogs.com/HappyBing/p/9777457.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!