zoukankan      html  css  js  c++  java
  • 装饰器的修订

    # 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)
  • 相关阅读:
    Leetcode Binary Tree Preorder Traversal
    Leetcode Minimum Depth of Binary Tree
    Leetcode 148. Sort List
    Leetcode 61. Rotate List
    Leetcode 86. Partition List
    Leetcode 21. Merge Two Sorted Lists
    Leetcode 143. Reorder List
    J2EE项目应用开发过程中的易错点
    JNDI初认识
    奔腾的代码
  • 原文地址:https://www.cnblogs.com/0B0S/p/11979341.html
Copyright © 2011-2022 走看看