美文网首页
Python无参装饰器

Python无参装饰器

作者: 那个小码哥 | 来源:发表于2017-02-28 11:51 被阅读0次
Python无参装饰器

需求:想要在test_func函数前后执行一些代码

  1. 第一步(定义函数,将调用原函数,使用新函数替换)
def test_func():
    return 'test_func'

def test_wrapper_func():
    print 'before'
    rt = test_func()
    print 'after'
    return rt
    
print test_wrapper_func()

2.第二 步(原函数可以定义参数)

def test_func():
    return 'test_func'

def test_wrapper_func(*args, **kwargs):
    print 'before'
    rt = test_func(*args, **kwargs)
    print 'after'
    return rt
    
  
print test_wrapper_func()

3.第三步(定义工厂函数,返回wapper函数)

def test_func():
    return 'test_func'


def test_wrapper_func(*args, **kwargs):
    print 'before'
    rt = test_func(*args, **kwargs)
    print 'after'
    return rt

def test_wrapper():
    return test_wrapper_func


test_wrapper_func_temp = test_wrapper()

print test_wrapper_func_temp == test_wrapper_func
print test_wrapper_func_temp()
print test_wrapper_func()

4.第四步(去掉共有的wapper函数,放在工厂函数内部)

def test_func():
    return 'test_func'


def test_wrapper():
    def wrapper(*args, **kwargs):
        print 'before'
        rt = test_func(*args, **kwargs)
        print 'after'
        return rt    
    return wrapper


test_wrapper_func = test_wrapper()
print test_wrapper_func()

5.第五步(可以为所有函数添加同一功能前后执行代码)

def test_func()
    return 'test_func'

def test_wrapper(func):
    def wrapper(*args, **kwargs):
        print 'before'
        rt = func(*args, **kwargs)
        print 'after'
        return rt
    return wrapper


test_wrapper_func = test_wrapper(test_func)
print test_wrapper_func()

6.第六步(使用装饰器)

def test_wrapper(func):
    def wrapper(*args, **kwargs):
        print 'before'
        rt = func(*args, **kwargs)
        print 'after'
        return rt    
    return wrapper
    

@test_wrapper
def test_func():
    return 'test_func'


print test_func()

7.第七步(修正多个装饰器时,使用函数fun名称问题)

from functools import wraps

def test_wrapper(func):    
    
    @wraps(func)
    def wrapper(*args, **kwargs):
        print 'before'
        rt = func(*args, **kwargs)
        print 'after'
        return rt    
    return wrapper
    
    
@test_wrapper
def test_func():
    return 'test_func'


print test_func()

相关文章

  • Python装饰器

    Python装饰器 一、函数装饰器 1.无参装饰器 示例:日志记录装饰器 2.带参装饰器 示例: 二、类装饰器 示例:

  • Decorator装饰器

    1. 基本装饰器 1.1. 无参方法无参装饰器 1.2. 有参方法无参装饰器 利用(*args, **kwa...

  • 函数 三

    无参装饰器模板 有参装饰器模板 叠加多个装饰器

  • 第四篇 闭包函数、装饰器、迭代器、序列化

    一、知识储备 二、闭包函数 三、简单装饰器 四、无参装饰器修订 五、无参装饰器之auth 六、有参装饰器 七、加多...

  • Python无参装饰器

    需求:想要在test_func函数前后执行一些代码 第一步(定义函数,将调用原函数,使用新函数替换) 2.第二 步...

  • 装饰器

    装饰器 decorator类装饰器 带参数的装饰器 举例(装饰器函数;装饰器类;有参与无参) https://fo...

  • 透析Python装饰器--透过现象看本质

    》眼花缭乱 Python的装饰器(也称语法糖)大致分为这几类: 无参数装饰器 有参数装饰器 装饰类的装饰器 无参数...

  • 透析Python装饰器-------------透过现象看本质

    》眼花缭乱 Python的装饰器(也称语法糖)大致分为这几类: 无参数装饰器 有参数装饰器 装饰类的装饰器 无参数...

  • decorator-2017-6-6

    Table of Contents 1. 什么是闭包 2. 装饰器 3. 无参装饰器 4...

  • 装饰器基础

    一、装饰器概述 装饰器(无参): 它是一个函数 函数作为它的形参 返回值也是一个函数 可以使用@function_...

网友评论

      本文标题:Python无参装饰器

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