美文网首页
python 装饰器decorator

python 装饰器decorator

作者: Karl_2c80 | 来源:发表于2018-12-04 16:45 被阅读0次
    • 闭包函数:函数内部定义的函数,引用了外部变量但非全局变量。
    In [24]: def count():
        ...:     a = 1
        ...:     b = 2
        ...:     def sum():
        ...:         c = 1
        ...:         return a + c
        ...:     return sum
        ...:
    

    装饰器 decorator

    • 实质: 是一个函数
    • 参数: 是你要装饰的函数名(非函数调用
    • 返回: 是装饰完的函数名(非函数调用
    • 作用: 为已存在的对象添加额外的功能
    • 特点: 无需对已存在对象做任何代码上的变动

    1. 原函数不带函数

    #!/usr/bin/python
    # -*- coding:utf-8 -*-
    
    def setTag(func):
        def wrapper():
            print 'It is a fine day!'
            func()
        return wrapper
    
    @setTag
    def hello():
        print 'Hello '
    
    hello()
    
    [root@KARL task_w2]# python decorator.py
    It is a fine day!
    Hello
    

    2. 原函数带参数

    #!/usr/bin/python
    # -*- coding:utf-8 -*-
    
    def setTag(func):
        def wrapper(*args, **kwargs):
            print 'It is a fine day!'
            func(*args, **kwargs)
        return wrapper
    
    @setTag
    def hello(name):
        print 'Hello '+name
    
    hello('Karl')
    
    [root@KARL task_w2]# python decorator.py
    It is a fine day!
    Hello Karl
    

    3. 原函数带参数且有返回值

    #!/usr/bin/python
    #coding:utf8
    
    def setTag(func):
        def wrapper(*args, **kwargs):
            print 'It is a fine day!'
            # res = func(*args, **kwargs)      # res来接收被装饰函数的返回值
            # return res                       # 这2行可合并为1行,如下
            return func(*args, **kwargs)
    
    @setTag         ## 相当于 hello = setTag(hello)
    def hello(name):
        return 'Hello '+name
    
    print hello('Karl')
    
    [root@KARL scripts]# python decorator.py
    It is a fine day!
    Hello Karl
    

    4. 装饰器带参数

    #!/usr/bin/python
    #coding:utf8
    
    def setTag(day):      # 1)先传装饰器的参数
        def wrapper(func):      # 2)再将被装饰函数作为参数传进来
            def inner_wrapper(*args, **kwargs):      # 3)最后传被装饰函数的参数
                print 'It is a fine day!'
                print 'Today is %s' %day
                return func(*args, **kwargs)
            return inner_wrapper
        return wrapper
    
    @setTag('Sunday')      # 等同于 hello = setTag(day='Sunday')(hello)
    def hello(name):
        return 'Hello '+name
    
    print hello('Karl')
    
    [root@KARL scripts]# python decorator.py
    It is a fine day!
    Today is Sunday
    Hello Karl
    

    5. 一个函数被多个装饰器装饰

    #!/usr/bin/python
    #coding:utf8
    
    def setTag(day):      # 1)先传装饰器的参数
        def wrapper(func):      # 2)再将被装饰函数作为参数传进来
            def inner_wrapper(*args, **kwargs):      # 3)最后传被装饰函数的参数
                print 'It is a fine day!'
                print 'Today is %s' %day
                return func(*args, **kwargs)
            return inner_wrapper
        return wrapper
    def myName(name):
        def outer(func):
            def inner(*args, **kwargs):
                print "My name's %s" %name
                return func(*args, **kwargs)
            return inner
        return outer
    
    @myName('Tom')   # 等同于 hello = myName(setTag(hello)),考虑参数则等同于 hello = myName(name='Tom')(setTag(day='Sunday')(hello))
    @setTag('Sunday')   # 等用于 hello = setTag(hello), 考虑参数则等同于 hello = setTag(day='Sunday')(hello)
    def hello(name):
        return 'Hello '+name
    
    print hello('Karl')
    
    [root@KARL scripts]# python multi_decorator.py
    My name's Tom
    It is a fine day!
    Today is Sunday
    Hello Karl
    

    相关文章

      网友评论

          本文标题:python 装饰器decorator

          本文链接:https://www.haomeiwen.com/subject/ootucqtx.html