zoukankan      html  css  js  c++  java
  • 装饰器

    一,为什么要使用装饰器

    装饰器是为了给某些函数添加一些通用功能,却不用去改变函数的源代码,例如给网页添加登录验证的功能。

    装饰器 = 高阶函数 + 函数嵌套 + 闭包

    二,高阶函数

    1.函数接收的参数是一个函数名
    2.函数的返回值是一个函数名
    3.满足上述条件任意一个,都可称之为高阶函数

    例如:

    def foo
        print('hello, python')
    
    def test(func):
          print(func)
          start_time=time.time()
          func()
          stop_time = time.time()
          print('函数运行时间是  %s' % (stop_time-start_time))
    
    test(foo)

    三,函数嵌套

    函数嵌套: 函数里面定义新的函数

    例如:

    def father(name):
    def son(): print('我的爸爸是%s' %name) def grandson(): print('我的爷爷是%s' %auth_type) grandson() son()
    file

    四,闭包

    闭包: 闭包的意思就是在子函数里面定义一个变量使得父函数的变量不影响子函数的变量。

    例如:

    def father(name):
        print('from father %s' %name)
        def son():
            name='linhaifeng'
            print('我的爸爸是%s' %name)
            def grandson():
                print('我的爷爷是%s' %auth_type)
            grandson()
        son()
    father('linlei')

    五,装饰器的实现

    import time
    def timmer(func): #func=test
        def wrapper():
            start_time=time.time()
            func() #就是在运行test()
            stop_time = time.time()
            print('运行时间是%s' %(stop_time-start_time))
        return wrapper
    
    @timmer #test=timmer(test)
    def test():
        time.sleep(3)
        print('test函数运行完毕')
    
    test()
    
    # test=timmer(test)  #返回的是wrapper的地址
    # test()  #执行的是wrapper()
    #  @timmer  就相当于 test=timmer(test)

    六,带返回值的函数的装饰器

    #__author: Administrator
    #__date: 2018/4/4/004
    import time
    def timmer(func):
        def wrapper():
            start_time=time.time()
            res=func()
            stop_time=time.time()
            print('运行时间是:%s'%(stop_time-start_time))
            return res
        return wrapper
    @timmer#test=timmer(test)
    def test():
        time.sleep(3)
        print('函数运行完毕')
        return '这是函数的返回值'
    
    res=test()
    print(res)

    七,带参数函数的实现

    import time
    def timmer(func): #func=test1
        def wrapper(*args,**kwargs): #test('linbi',age=18)  args=('linhaifeng')  kwargs={'age':18}
            start_time=time.time()
            res=func(*args,**kwargs) #就是在运行test()         func(*('linbi'),**{'age':18})
            stop_time = time.time()
            print('运行时间是%s' %(stop_time-start_time))
            return res
        return wrapper
    
    # @timmer #test=timmer(test)
    def test(name,age):
        time.sleep(3)
        print('test函数运行完毕,名字是【%s】 年龄是【%s】' %(name,age))
        return '这是test的返回值'
    
    @timmer
    def test1(name,age,gender):
        time.sleep(1)
        print('test1函数运行完毕,名字是【%s】 年龄是【%s】 性别【%s】' %(name,age,gender))
        return '这是test的返回值'
    
    res=test('linbi',age=18)  #就是在运行wrapper
    print(res)
    
    test1('liming',18,'male')
  • 相关阅读:
    Binary Search Tree Iterator 解答
    Invert Binary Tree 解答
    Min Stack 解答
    Trapping Raining Water 解答
    Candy 解答
    Jump Game II 解答
    Implement Hash Map Using Primitive Types
    Gas Station 解答
    Bucket Sort
    HashMap 专题
  • 原文地址:https://www.cnblogs.com/ithome0222/p/8719164.html
Copyright © 2011-2022 走看看