装饰器 decorator (python)
2014-08-19 04:01
330 查看
装饰器, 根据名字理解就是给函数的功能上做一些装饰(增加功能)
与类的继承相比 好处:
1. 动态: 如果用类的继承,就是在代码的层面上进行添加。 而装饰器是动态的
2. 灵活: 在一个类中,我们保留核心的功能,那些可有可无的功能,我们最好不添加进去(防止类太臃肿)
但当要加的功能比较零碎的时候,他们的排列组合会非常的多,此时子类的个数也会非常的多
而用动态的装饰器就比较合适。
事例代码,主要参考:
http://www.cnblogs.com/rhcad/archive/2011/12/21/2295507.html http://www.cnblogs.com/huxi/archive/2011/03/01/1967600.html http://blog.csdn.net/thy38/article/details/4471421
与类的继承相比 好处:
1. 动态: 如果用类的继承,就是在代码的层面上进行添加。 而装饰器是动态的
2. 灵活: 在一个类中,我们保留核心的功能,那些可有可无的功能,我们最好不添加进去(防止类太臃肿)
但当要加的功能比较零碎的时候,他们的排列组合会非常的多,此时子类的个数也会非常的多
而用动态的装饰器就比较合适。
事例代码,主要参考:
http://www.cnblogs.com/rhcad/archive/2011/12/21/2295507.html http://www.cnblogs.com/huxi/archive/2011/03/01/1967600.html http://blog.csdn.net/thy38/article/details/4471421
相关文章推荐
- Python decorator(装饰器)
- 深入理解Python 装饰器(decorator)
- Head First 设计模式——装饰器(Decorator Pattern)——Python实现
- Python 装饰器(decorator )的深度解读
- Python 装饰器(Decorator)
- python里为什么需要使用装饰器(decorator)
- python装饰器decorator
- Python装饰器 decorator 设计模式
- 对Python中装饰器(Decorator)的理解与进阶
- 深入理解Python 装饰器(decorator)
- Python中的Decorator(装饰器)模式
- Python如何使用装饰器(Decorator)修改yield function返回值
- 装饰器学习(2)------深入理解Python 装饰器(decorator)
- python decorator(装饰器)
- Python 装饰器( Decorator)
- python 高阶函数:Decorator(装饰器)
- python3 第二十二章 - 函数式编程之Decorator(装饰器)
- Python的walk和Decorator使用一例
- python self,cls,decorator的理解
- 轻量级AOP框架-移植python的装饰器(Decorator)到C#(编码篇)