浅析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 相关文章推荐
pyqt4教程之实现半透明的天气预报界面示例
Mar 02 Python
python通过微信发送邮件实现电脑关机
Jun 20 Python
解决python中os.listdir()函数读取文件夹下文件的乱序和排序问题
Oct 17 Python
一个可以套路别人的python小程序实例代码
Apr 09 Python
python序列类型种类详解
Feb 26 Python
简单了解python shutil模块原理及使用方法
Apr 28 Python
高考考python编程是真的吗
Jul 20 Python
Python实现敏感词过滤的4种方法
Sep 12 Python
Pycharm操作Git及GitHub的步骤详解
Oct 27 Python
Python字典dict常用方法函数实例
Nov 09 Python
这样写python注释让代码更加的优雅
Jun 02 Python
pytorch 权重weight 与 梯度grad 可视化操作
Jun 05 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基础知识:类与对象(1)
2006/12/13 PHP
php实现简单的上传进度条
2015/11/17 PHP
PHP实现简单的计算器
2020/08/28 PHP
学习ExtJS(一) 之基础前提
2009/10/07 Javascript
写给想学习Javascript的朋友一点学习经验小结
2010/11/23 Javascript
js DOM的学习笔记
2011/12/22 Javascript
js 处理数组重复元素示例代码
2013/12/27 Javascript
jQuery中prepend()方法使用详解
2015/08/11 Javascript
js正则表达式最长匹配(贪婪匹配)和最短匹配(懒惰匹配)用法分析
2016/12/27 Javascript
vue router路由嵌套不显示问题的解决方法
2017/06/17 Javascript
AngularJS通过ng-Img-Crop实现头像截取的示例
2017/08/17 Javascript
zTree jQuery 树插件的使用(实例讲解)
2017/09/25 jQuery
vuex的简单使用教程
2018/02/02 Javascript
layui扩展上传组件模拟进度条的方法
2019/09/23 Javascript
vue中使用rem布局代码详解
2019/10/30 Javascript
Python实现批量将word转html并将html内容发布至网站的方法
2015/07/14 Python
python 正确保留多位小数的实例
2018/07/16 Python
python pygame实现2048游戏
2018/11/20 Python
python多任务及返回值的处理方法
2019/01/22 Python
python ChainMap的使用和说明详解
2019/06/11 Python
python3 线性回归验证方法
2019/07/09 Python
Django的models中on_delete参数详解
2019/07/16 Python
Python实现密码薄文件读写操作
2019/12/16 Python
Python:type、object、class与内置类型实例
2019/12/25 Python
Python破解BiliBili滑块验证码的思路详解(完美避开人机识别)
2020/02/17 Python
Keras中的两种模型:Sequential和Model用法
2020/06/27 Python
移动端rem布局的两种实现方法
2018/01/03 HTML / CSS
HTML5离线应用与客户端存储的实现
2018/05/03 HTML / CSS
德国家用电器购物网站:Premiumshop24
2019/08/22 全球购物
初级会计求职信范文
2014/02/15 职场文书
财务工作犯错检讨书
2014/10/07 职场文书
少先队辅导员事迹材料
2014/12/24 职场文书
归元寺导游词
2015/02/06 职场文书
2016优秀护士求职自荐信
2016/01/28 职场文书
4种非常实用的python内置数据结构
2021/04/28 Python
oracle覆盖导入dmp文件的2种方法
2021/05/21 Oracle