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!') say_hello() """ [DEBUG]:entersay_hello() hello! """
以上是python wrapper的介绍,大家在python中还是比较容易遇到装饰的使用,看完内容后可以做一些练习。更多Python学习指导:python基础教程
(推荐操作系统:windows7系统Python 3.9.1,DELL G3电脑。)