您现在的位置是:首页 > Python 当前栏目 python之装饰器 Python 2023-03-02 11:11:38 时间 1.概念:是一个闭包,把一个函数当作参数返回一个代替版的函数,本质上就是一个返回函数的函数. 2.理解:在不改变原函数的基础上给函数增加新的功能 3.例一(一个简单的装饰器): def func(): print("machi is a good man!") def outer(func1): def inner(): print("*********") func1() return inner func = outer(func) func() 4.例二(一个较复杂的装饰器): def say(age): print("machi is a %d years old man!) %(age)) def outer(func): def inner(a): if a >= 0: func(age) else: func(0) return inner say = outer(say) say(10) say(-10) 5.通用装饰器(模板): def outer(func): def inner(*args, **kwargs): #表示可以接受任意数量的参数 #添加装饰器功能 pass return inner @outer #应用装饰器 本文地址: python之装饰器 相关文章 python数据保存 Python图片裁剪 java 0day_Java 0day简略分析 python 闭包 Python - 闭包 python 闭包 python 四边形分割 python 旋转列表 【python】入门oj Python打包工具 Python常见错误 python初探(一) 190707Python-RabbitMQ python 虚拟环境 python的GIL python-问题解决 python函数讲座 MysQL连接python python ——时间间隔 python连接Mongodb