• 装饰器的修订


    # import time
    #
    # def index():
    # time.sleep(3)
    # print('Welcome to China')
    # def timmer(func):
    # def inner():
    # start = time.time()
    # func()
    # stop = time.time()
    # print('run time is %s' % (stop - start))
    # return inner
    #
    # index=timmer(index)
    # index()
    '''
    Welcome to China
    run time is 3.0002970695495605
    '''
    # 装饰器语法:在被装饰对象正上方一行单独写上@装饰器名
    # import time
    # def timmer(func):
    # def inner():
    # start = time.time()
    # func()
    # stop = time.time()
    # print('run time is %s' % (stop - start))
    # return inner
    # @timmer # 把@装饰器正下方的函数名当作参数传给@装饰器名,并把结果重新命名给index
    # def index():
    # time.sleep(3)
    # print('Welcome to China')
    #
    # index()
    '''
    Welcome to China
    run time is 3.000959634780884
    '''
    # import time
    # def timmer(func):
    # def inner():
    # start = time.time()
    # res=func()
    # stop = time.time()
    # print('run time is %s' % (stop - start))
    # return res
    # return inner
    # @timmer
    # def index():
    # time.sleep(1)
    # print('Welcome to China')
    # return 521
    #
    # res=index()
    # print(res)
    '''
    Welcome to China
    run time is 1.0006263256072998
    521
    '''
    # import time
    # def timmer(func):
    # def inner(*args,**kwargs):
    # start = time.time()
    # res=func(*args,**kwargs)
    # stop = time.time()
    # print('run time is %s' % (stop - start))
    # return res
    # return inner
    # @timmer
    # def home(name):
    # print('Welcome %s to China'%name)
    #
    # home('Rambo')
    '''
    Welcome Rambo to China
    run time is 0.0
    '''
    # 有参装饰器
    # import time
    # def auth(func):
    # # func=index
    # def inner(*args,**kwargs):
    # name=input('>>:name').strip()
    # password=input(">>:paseword").strip()
    # if name == "Rambo" and password == '123':
    # print('登录成功')
    # return func(*args,**kwargs)
    # else:
    # print('用户名或者密码错误')
    # return inner
    # @auth
    # def index(name):
    # time.sleep(1)
    # print('Welcome %s to China'%name)
    # return 521
    #
    # res=index('Rambo')
    # print(res)
  • 相关阅读:
    删除前添加确认删除弹出框
    virtualbox 网络设置
    JavaScript跨域总结与解决办法
    分享一个ci 框架下取不到cookie的问题
    firebug console说明
    innodb 修改表共享空间为独立空间
    grunt 试用笔记
    apache TIME_WAIT解决办法
    负载均衡情况下获取真实ip的方法
    linux挂载硬盘
  • 原文地址:https://www.cnblogs.com/0B0S/p/11979341.html
Copyright © 2020-2023  润新知