zoukankan      html  css  js  c++  java
  • 闭包和装饰器

    闭包

    函数的引用

    def fun():
        print("---in fun---")
    
    #把函数的引用给ret变量,即ret指向了这个对象
    >>> ret = fun
    >>> ret
    <function fun at 0x0242C6F0>
    >>> fun
    <function fun at 0x0242C6F0>
    #通过引用调用函数
    >>> ret()
    ---in fun---
    

    什么是闭包

    #定义一个函数
    def test(number):
    
        #在函数内部再定义一个函数,并且这个函数用到了外边函数的变量,那么将这个函数以及用到的一些变量称之为闭包
        def test_in(number_in):
            print("in test_in 函数, number_in is %d"%number_in)
            return number+number_in
        #返回内部函数的引用
        return test_in
    
    
    #给test函数赋值,这个20就是给参数number
    ret = test(20)
    
    #注意这里的100其实给参数number_in
    print(ret(100))
    
    #注意这里的200其实给参数number_in
    print(ret(200))
    

    运行结果

    in test_in 函数, number_in is 100
    120
    
    in test_in 函数, number_in is 200
    220
    

    nonlocal

    global适用于函数内部修改全局变量的值
    nonlocal适用于嵌套函数中内部函数修改外部变量的值
    如果没有使用以上关键字,对全局变量或者外部变量进行修改,python会默认将全局变量隐藏起来

    def counter(start=0):
        def incr():
            # 引用外部变量,不加关键字nonlocal会重新隐藏外部变量的值
            nonlocal start
            start += 1
            return start
        return incr
        
    c1 = counter(5)
    print(c1())
    print(c1())
    
    结果:
    6,7
    

    闭包的作用

    def line_conf(a, b):
        def line(x):
            return a*x + b
        return line
    #绘制不同的直线
    line1 = line_conf(1, 1)
    line2 = line_conf(4, 5)
    print(line1(5))
    print(line2(5))
    

    函数line与变量a,b构成闭包。在创建闭包的时候,我们通过line_conf的参数a,b说明了这两个变量的取值,这样,我们就确定了函数的最终形式(y = x + 1和y = 4x + 5)。我们只需要变换参数a,b,就可以获得不同的直线表达函数。由此,我们可以看到,闭包也具有提高代码可复用性的作用

    闭包思考:

    1. 闭包似优化了变量,原来需要类对象完成的工作,闭包也可以完成
    2. 由于闭包引用了外部函数的局部变量,则外部函数的局部变量没有及时释放,消耗内存

    装饰器

    def verify(func):
        def inner():
            # 验证1
            # 验证2
            # 验证3
            func()
        return inner
    
    @verify
    def f1():
        print('f1')
    

    python解释器就会从上到下解释代码,步骤如下
    1. def w1(func): ==>将w1函数加载到内存
    2. @w1:
    -执行w1函数 ,并将 @w1 下面的函数作为w1函数的参数,即:@w1 等价于 w1(f1)
    -将执行完的w1函数返回值 赋值 给@w1下面的函数的函数名f1 即将w1的返回值再重新赋值给 f1

    装饰器(decorator)功能

    -引入日志
    -函数执行时间统计
    -执行函数前预备处理
    -执行函数后清理功能
    -权限校验等场景
    -缓存

    装饰器示例

    例1:无参数的函数

    from time import ctime, sleep
    
    def timefun(func):
        def wrappedfunc():
            print("%s called at %s"%(func.__name__, ctime()))
            func()
        return wrappedfunc
    
    @timefun
    def foo():
        print("I am foo")
    
    foo()
    sleep(2)
    foo()
    

    上面代码理解装饰器执行行为可理解成:
    foo = timefun(foo)
    foo先作为参数赋值给func后,foo接收指向timefun返回的wrappedfunc

    foo()
    调用foo(),即等价调用wrappedfunc()
    内部函数wrappedfunc被引用,所以外部函数的func变量(自由变量)并没有释放
    func里保存的是原foo函数对象

    例2:被装饰的函数有参数

    from time import ctime, sleep
    
    def timefun(func):
        def wrappedfunc(a, b):
            print("%s called at %s"%(func.__name__, ctime()))
            print(a, b)
            func(a, b)
        return wrappedfunc
    
    @timefun
    def foo(a, b):
        print(a+b)
    
    foo(3,5)
    sleep(2)
    foo(2,4)
    

    例3:被装饰的函数有不定长参数

    from time import ctime, sleep
    
    def timefun(func):
        def wrappedfunc(*args, **kwargs):
            print("%s called at %s"%(func.__name__, ctime()))
            func(*args, **kwargs)
        return wrappedfunc
    
    @timefun
    def foo(a, b, c):
        print(a+b+c)
    
    foo(3,5,7)
    sleep(2)
    foo(2,4,9)
    

    例4:装饰器中的return

    from time import ctime, sleep
    
    def timefun(func):
        def wrappedfunc():
            print("%s called at %s"%(func.__name__, ctime()))
            # 虽然原函数有返回值,但在这里,没有return,所以返回值为None
            func()
        return wrappedfunc
    
    @timefun
    def foo():
        print("I am foo")
    
    @timefun
    def getInfo():
        return '----hahah---'
    
    foo()
    sleep(2)
    print(getInfo()) #先执行函数,再打印返回值
    

    执行结果

    foo called at Fri Nov  4 21:55:35 2016
    I am foo
    
    getInfo called at Fri Nov  4 21:55:37 2016
    None
    

    总结:

    一般情况下为了让装饰器更通用,可以加return

    例5:装饰器带参数,在原有装饰器的基础上,设置外部变量

    from time import ctime, sleep
    
    def timefun_arg(pre="hello"):
        def timefun(func):
            def wrappedfunc():
                print("%s called at %s %s"%(func.__name__, ctime(), pre))
                return func()
            return wrappedfunc
        return timefun
    
    @timefun_arg("itcast")
    def f1():
        print("I am f1")
    
    @timefun_arg("python")
    def f2():
        print("I am f2")
    
    f1()
    sleep(2)
    f2()
    

    例6:类装饰器

    装饰器函数其实是这样一个接口约束,它必须接受一个callable对象作为参数,然后返回一个callable对象。在Python中一般callable对象都是函数,但也有例外。只要某个对象重写了 call() 方法,那么这个对象就是callable的。

    class Test():
        def __call__(self):
            print('call me!')
    
    t = Test()
    t()  # call me
    

    类装饰器demo

    class Test(object):
        def __init__(self, func):
            print("---初始化---")
            print("func name is %s"%func.__name__)
            self.__func = func
        def __call__(self):
            print("---装饰器中的功能---")
            self.__func()
    
    @Test
    def test():
        print("----test---")
    test()
    #说明:
    #1. 当用Test来装作装饰器对test函数进行装饰的时候,首先会创建Test的实例对象
    #    并且会把test这个函数名当做参数传递到__init__方法中
    #    即在__init__方法中的func变量指向了test函数体
    #
    #2. test函数相当于指向了用Test创建出来的实例对象
    #
    #3. 当在使用test()进行调用时,就相当于让这个对象(),因此会调用这个对象的__call__方法
    #
    #4. 为了能够在__call__方法中调用原来test指向的函数体,所以在__init__方法中就需要一个实例属性来保存这个函数体的引用
    #    所以才有了self.__func = func这句代码,从而在调用__call__方法中能够调用到test之前的函数体
    

    运行结果如下:

    ---初始化---
    func name is test
    ---装饰器中的功能---
    ----test---
    
  • 相关阅读:
    USACO2.3.3Zero Sum
    微信公众平台消息接口开发(8)小黄鸡(小贱鸡)机器人
    微信群二维码
    PHP正则表达式入门教程[转]
    微信公众平台消息接口开发(23)图片识别之男人味/女人味指数
    微信公众平台消息接口开发(50)在线点歌/在线音乐
    微信公众平台消息接口开发(21)图片识别之亲子鉴定
    微信公众平台消息接口开发(4)天气预报
    微信公众平台消息接口开发(5)股票查询
    微信公众平台消息接口开发(40)语音识别
  • 原文地址:https://www.cnblogs.com/shuai-long/p/9592128.html
Copyright © 2011-2022 走看看