浅析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程序语言快速上手教程
Jul 18 Python
Python中使用Boolean操作符做真值测试实例
Jan 30 Python
Python2.x版本中cmp()方法的使用教程
May 14 Python
详解Python中time()方法的使用的教程
May 22 Python
Python IDLE入门简介
Dec 08 Python
python在TXT文件中按照某一字符串取出该字符串所在的行方法
Dec 10 Python
对python for 文件指定行读写操作详解
Dec 29 Python
python getpass实现密文实例详解
Sep 24 Python
Python基于requests库爬取网站信息
Mar 02 Python
对django 2.x版本中models.ForeignKey()外键说明介绍
Mar 30 Python
Django 如何使用日期时间选择器规范用户的时间输入示例代码详解
May 22 Python
用python发送微信消息
Dec 21 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读取txt文件的内容并赋值给数组的代码
2011/11/03 PHP
PHP 利用Mail_MimeDecode类提取邮件信息示例
2014/01/26 PHP
php的hash算法介绍
2014/02/13 PHP
PHP中几个可以提高运行效率的代码写法、技巧分享
2014/08/21 PHP
php实现将字符串按照指定距离进行分割的方法
2015/03/14 PHP
php通过获取头信息判断图片类型的方法
2015/06/26 PHP
Windows2003下php5.4安装配置教程(IIS)
2016/06/30 PHP
几个javascript操作word的参考代码
2009/10/26 Javascript
经典海量jQuery插件 大家可以收藏一下
2010/02/07 Javascript
初学js插入节点appendChild insertBefore使用方法
2011/07/04 Javascript
js判断IE浏览器版本过低示例代码
2013/11/22 Javascript
JavaScript中判断页面关闭、页面刷新的实现代码
2014/08/27 Javascript
javascript实现倒计时(精确到秒)
2015/06/26 Javascript
js实现的tab标签切换效果代码分享
2015/08/25 Javascript
解决JavaScript数字精度丢失问题的方法
2015/12/03 Javascript
正则表达式(语法篇推荐)
2016/06/24 Javascript
AngularJS表单详解及示例代码
2016/08/17 Javascript
详解JS数据类型的值拷贝函数(深拷贝)
2017/07/13 Javascript
nodejs中art-template模板语法的引入及冲突解决方案
2017/11/07 NodeJs
详解关于Vue版本不匹配问题(Vue packages version mismatch)
2018/09/17 Javascript
10个最受欢迎的 JavaScript框架(推荐)
2019/04/24 Javascript
Python struct.unpack
2008/09/06 Python
跟老齐学Python之大话题小函数(2)
2014/10/10 Python
python爬虫爬取淘宝商品信息
2018/02/23 Python
Django框架实现逆向解析url的方法
2018/07/04 Python
获取django框架orm query执行的sql语句实现方法分析
2019/06/20 Python
解决Django migrate不能发现app.models的表问题
2019/08/31 Python
Django日志及中间件模块应用案例
2020/09/10 Python
python中xlutils库用法浅析
2020/12/29 Python
医学毕业生自我鉴定
2013/10/30 职场文书
优秀安全员事迹材料
2014/05/11 职场文书
护理专科学生自荐书
2014/07/05 职场文书
单位委托书怎么写
2014/09/21 职场文书
2015年护士节慰问信
2015/03/23 职场文书
golang中的struct操作
2021/11/11 Golang
全新239军机修复记
2022/04/05 无线电