python装饰器基本模板

python装饰器基础模板

def decorator_name(func):
    def wrapper(*args, **kwargs):
        print('在装饰器之前执行的代码')

        """执行被装饰器函数"""
        result = func(*args, **kwargs)

        print('在装饰器之后执行的代码')
        return result

    return wrapper


@decorator_name
def my_function():
    pass

 

获取被装饰函数的输入、输出 参数

def decorator_name(func):
    def wrapper(*args, **kwargs):
        print(f'获取被装饰函数的输入参数:{args}')

        """执行被装饰器函数"""
        result = func(*args, **kwargs)

        print(f'获取被装饰函数的输出参数:{result}')
        return result

    return wrapper


@decorator_name
def my_function(a, b):
    return a, b


my_function('你好', '中国')

 

posted @ 2021-09-28 17:04  博无止境  阅读(19)  评论(0编辑  收藏  举报