1、说明
wrapper是装饰器的意思,装饰器本质上是一个Python函数。可以让其他函数,在不需要做任何代码变动的前提下增加额外功能,装饰器的返回值也是一个函数对象。
2、应用场景
插入日志、性能测试、事务处理、缓存、权限校验等
3、实例
无参数的装饰器。
defdebug(func): defwrapper(): print('[DEBUG]:enter{}()'.format(func.__name__)) returnfunc() returnwrapper @debug defsay_hello(): print('hello!') say_hello() """ [DEBUG]:entersay_hello() hello! """
以上就是python wrapper的介绍,大家在python中还是比较容易遇到装饰器的使用,可以在看完内容后做一些练习。
(推荐操作系统:windows7系统、Python 3.9.1,DELL G3电脑。)