浅析Python装饰器以及装饰器模式


Posted in Python onMay 28, 2018

漫谈

如果作为一个Python入门,不了解Python装饰器也没什么,但是如果作为一个中级Python开发人员,如果再不对python装饰器熟稔于心的话,那么可能并没有量变积累到质变。

我以前也看过很多讲python 装饰器的文章,但是都是看了就忘。一方面是没有做太多的练习,二是对它的领会不是很深。

希望引以为戒!!!

郑传

装饰模式

如果你了解Java,你肯定听过 装饰器模式。在面向对象中,装饰模式指:动态地给一个对象添加一些额外的职责。就增加一些功能来说,装饰模式比生成子类更为灵活。

在设计模式学习----装饰器模式,我摘取了下面一段使用装饰器模式的代码

public class DecoratorPattern { 
 
  /** 
   * @param args the command line arguments 
*/ 
  public static void main(String[] args) { 
    // TODO code application logic here 
    Basket basket = new Original(); 
    //一个装饰的过程 
    Basket myBasket =new AppleDecorator(new BananaDecorator(new OrangeDecorator(basket)));  
    myBasket.show(); 
  } 
}

等会注意下 Basket myBasket =new AppleDecorator(new BananaDecorator(new OrangeDecorator(basket))) 这段的写法

在Python官方文档PythonDecorators 是这么介绍装饰器的

What is a Decorator
A decorator is the name used for a software design pattern. Decorators dynamically alter the functionality of a function, method, or class without having to directly use subclasses or change the source code of the function being decorated.

翻一下: 就是装饰器是一种软件设计模式,被用来动态修改函数、方法,或者类功能却不是通过子类,或者修改原代码实现。

跟之前是一个意思!!!

Python Decorator
而Python的装饰器与之不同,官方这么说:

The "decorators" we talk about with concern to Python are not exactly the same thing as the DecoratorPattern described above. A Python decorator is a specific change to the Python syntax that allows us to more conveniently alter functions and methods (and possibly classes in a future version). This supports more readable applications of the DecoratorPattern but also other uses as well.
Support for the decorator syntax was proposed for Python in PEP 318, and will be implemented in Python 2.4.

翻译下:Python的 decorators 与 DecoratorPattern并不完全相同。 Python的decorator是一种特殊:在语法上实现允许我们更灵活地更改方法,或者函数。

例子:

@classmethod
def foo (arg1, arg2):
  ....

记住这个特殊的语法,后面我们会展示这个强大的语法糖

Python 相关文章推荐
python判断、获取一张图片主色调的2个实例
Apr 10 Python
Python3访问并下载网页内容的方法
Jul 28 Python
深度定制Python的Flask框架开发环境的一些技巧总结
Jul 12 Python
python删除过期log文件操作实例解析
Jan 31 Python
django js实现部分页面刷新的示例代码
May 28 Python
python切片(获取一个子列表(数组))详解
Aug 09 Python
python打印直角三角形与等腰三角形实例代码
Oct 20 Python
Django之form组件自动校验数据实现
Jan 14 Python
python 解决pycharm运行py文件只有unittest选项的问题
Sep 01 Python
Python数据库封装实现代码示例解析
Sep 05 Python
python中_del_还原数据的方法
Dec 09 Python
Python实现仓库管理系统
May 30 Python
Python装饰器知识点补充
May 28 #Python
更换Django默认的模板引擎为jinja2的实现方法
May 28 #Python
django manage.py扩展自定义命令方法
May 27 #Python
python实现windows下文件备份脚本
May 27 #Python
django 解决manage.py migrate无效的问题
May 27 #Python
关于django 数据库迁移(migrate)应该知道的一些事
May 27 #Python
解决Django migrate No changes detected 不能创建表的问题
May 27 #Python
You might like
php中转义mysql语句的实现代码
2011/06/24 PHP
php防注入,表单提交值转义的实现详解
2013/06/10 PHP
php实现mysql数据库操作类分享
2014/02/14 PHP
基于jQuery的表格操作插件
2010/04/22 Javascript
围观tangram js库
2010/12/28 Javascript
javascript中关于执行环境的杂谈
2011/08/14 Javascript
JS实现图片横向滚动效果示例代码
2013/09/04 Javascript
jQuery对Select的操作大集合(收藏)
2013/12/28 Javascript
jquery.post用法关于type设置问题补充
2014/01/03 Javascript
JavaScript实现生成GUID(全局统一标识符)
2014/09/05 Javascript
jQuery简单实现QQ空间点赞已经取消点赞
2015/04/02 Javascript
jquery仅用6行代码实现滑动门效果
2015/09/07 Javascript
JavaScript检测原始值、引用值、属性
2016/06/20 Javascript
JavaScript对象创建模式实例汇总
2016/10/03 Javascript
JS获取子、父、兄节点方法小结
2017/08/14 Javascript
node.js利用mongoose获取mongodb数据的格式化问题详解
2017/10/06 Javascript
axios全局请求参数设置,请求及返回拦截器的方法
2018/03/05 Javascript
nodejs实现解析xml字符串为对象的方法示例
2018/03/14 NodeJs
JavaScript对象拷贝与Object.assign用法实例分析
2018/06/20 Javascript
解决vue2 在mounted函数无法获取prop中的变量问题
2018/11/15 Javascript
浅谈Vue.js组件(二)
2019/04/09 Javascript
js全屏事件fullscreenchange 实现全屏、退出全屏操作
2019/09/17 Javascript
js实现倒计时秒杀效果
2020/03/25 Javascript
[51:26]DOTA2上海特级锦标赛主赛事日 - 2 胜者组第一轮#3Secret VS OG第二局
2016/03/03 DOTA
mac下如何将python2.7改为python3
2018/07/13 Python
在Python中给Nan值更改为0的方法
2018/10/30 Python
Python3自定义http/https请求拦截mitmproxy脚本实例
2020/05/11 Python
Python 开发工具通过 agent 代理使用的方法
2020/09/27 Python
泰国综合购物网站:Lazada泰国
2018/04/09 全球购物
社区国庆节活动方案
2014/02/05 职场文书
教师自我反思材料
2014/02/14 职场文书
总经理任命书范本
2014/06/05 职场文书
学校春季防火方案
2014/06/08 职场文书
教室布置标语
2014/06/26 职场文书
浅谈MySQL之select优化方案
2021/08/07 MySQL
配置Kubernetes外网访问集群
2022/03/31 Servers