您的当前位置:首页正文

python wrapper是什么

来源:花图问答

1、说明

wrapper是装饰器的意思,装饰器本质上是一个Python函数。可以让其他函数,在不需要做任何代码变动的前提下增加额外功能,装饰器的返回值也是一个函数对象。

2、应用场景

插入日志、性能测试、事务处理、缓存、权限校验等

3、实例

无参数的装饰器。

def debug(func):
    def wrapper():
        print('[DEBUG]: enter {}()'.format(func.__name__))
        return func()
    return wrapper
 
@debug
def say_hello():
    print('hello!')
say_hello()
"""
[DEBUG]: enter say_hello()
hello!
"""

以上就是python wrapper的介绍,大家在python中还是比较容易遇到装饰器的使用,可以在看完内容后做一些练习。更多Python学习指路:

(推荐操作系统:windows7系统、Python 3.9.1,DELL G3电脑。)