python 装饰器
来源:互联网 发布:warframe离线数据模块 编辑:程序博客网 时间:2024/06/08 08:32
装饰器介绍
装饰器(decorator)是一种高级Python语法。装饰器可以对一个函数、方法或者类进行加工。在Python中,我们有多种方法对函数和类进行加工,比如在Python闭包中,我们见到函数对象作为某一个函数的返回结果。相对于其它方式,装饰器语法简单,代码可读性高。因此,装饰器在Python项目中有广泛的应用。
装饰器最早在Python 2.5中出现,它最初被用于加工函数和方法这样的可调用对象(callable object,这样的对象定义有call方法)。在Python 2.6以及之后的Python版本中,装饰器被进一步用于加工类。
装饰器的用法:
#coding:utf-8def outer(fun): def wrapper(): print '验证' fun() print '嘻嘻嘻' return wrapper@outer def Func1(): print "看看输出的结果"Func1()
可以把 @outer 理解为如下:
Func1 = def wrapper(): print '验证' fun() print '嘻嘻嘻'
以上代码相当于如下程序代码:
#coding:utf-8def outer(fun): def wrapper(): print '验证' fun() print '嘻嘻嘻' return wrapperdef wrapper(): print '验证' fun() print '嘻嘻嘻'def Func1(): print "看看输出的结果"Func1()
0 0
- python装饰器装饰类
- [python]python装饰器
- Python装饰器学习
- Python装饰器
- python 装饰器
- Python装饰器
- python 装饰器 应用
- python 装饰器2
- Python装饰器学习
- python装饰器
- python装饰器
- python 装饰器入门
- python装饰器
- python装饰器
- python 装饰器'@'
- python装饰器
- Python装饰器详解
- python 装饰器
- Android按钮事件响应顺序
- MySQL集群搭建详解
- ubunut recovery mode 下拷贝数据到u盘
- 使用TCP协议的NAT穿透技术
- PCH文件写输出的宏
- python 装饰器
- 关于上上文hashmap的深入-hashmap产生死锁的详解
- Team Queue(queue) - uva 540
- 剑指offer-数据流中的中位数
- js-json的数据转化及正则表达式的运用
- 46.Majority Number-主元素(容易题)
- 堆!!!--Java实现
- Spark学习笔记
- iOS开发,获取手机型号,系统版本号