浅析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反射的用法实例分析
Feb 11 Python
python输入整条数据分割存入数组的方法
Nov 13 Python
详解python 模拟豆瓣登录(豆瓣6.0)
Apr 18 Python
Python 循环终止语句的三种方法小结
Jun 24 Python
python+tkinter实现学生管理系统
Aug 20 Python
python getpass模块用法及实例详解
Oct 07 Python
python使用正则表达式(Regular Expression)方法超详细
Dec 30 Python
pytorch实现mnist分类的示例讲解
Jan 10 Python
django在保存图像的同时压缩图像示例代码详解
Feb 11 Python
python使用paramiko实现ssh的功能详解
Mar 06 Python
Django models文件模型变更错误解决
May 11 Python
详解PyTorch模型保存与加载
Apr 28 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面向对象全攻略 (五) 封装性
2009/09/30 PHP
PHP新手用的Insert和Update语句构造类
2012/03/31 PHP
CodeIgniter输出中文乱码的两种解决办法
2014/06/12 PHP
JS中style属性
2006/10/11 Javascript
关于实现代码语法标亮 dp.SyntaxHighlighter
2007/02/02 Javascript
记录几个javascript有关的小细节
2007/04/02 Javascript
谷歌浏览器 insertCell与appendChild的区别
2009/02/12 Javascript
使用滤镜设置透明导致 IE 6/7/8/9 解析异常的解决方法
2011/04/07 Javascript
图片动画横条广告带上下滚动可自定义图片、链接等等
2013/10/20 Javascript
jQuery事件绑定on()、bind()与delegate() 方法详解
2015/06/03 Javascript
详解jQuery向动态生成的内容添加事件响应jQuery live()方法
2015/11/02 Javascript
JavaScript如何获取数组最大值和最小值
2015/11/18 Javascript
jQueryUI DatePicker 添加时分秒
2016/06/04 Javascript
jQuery.datatables.js插件用法及api实例详解
2016/10/28 Javascript
jQuery.parseHTML() 函数详解
2017/01/09 Javascript
使用travis-ci如何持续部署node.js应用详解
2017/07/30 Javascript
AngularJs每天学习之总体介绍
2017/08/07 Javascript
从0到1构建vueSSR项目之路由的构建
2019/03/07 Javascript
layui表格 列自动适应大小失效的解决方法
2019/09/06 Javascript
Vue结合路由配置递归实现菜单栏功能
2020/06/16 Javascript
Python cookbook(数据结构与算法)找出序列中出现次数最多的元素算法示例
2018/03/15 Python
python如何定义带参数的装饰器
2018/03/20 Python
python模块smtplib实现纯文本邮件发送功能
2018/05/22 Python
解决在pycharm中显示额外的 figure 窗口问题
2019/01/15 Python
PyTorch中的padding(边缘填充)操作方式
2020/01/03 Python
如何基于Python实现数字类型转换
2020/02/07 Python
virtualenv介绍及简明教程
2020/06/23 Python
python开发一个解析protobuf文件的简单编译器
2020/11/17 Python
回馈慈善的设计师太阳镜:DIFF eyewear
2019/10/17 全球购物
护士求职推荐信范文
2013/11/23 职场文书
职业规划书如何设计?
2014/01/09 职场文书
优秀家长自荐材料
2014/08/26 职场文书
产品调价通知函
2015/04/20 职场文书
导游词之江西赣州
2019/10/15 职场文书
sqlserver2017共享功能目录路径不可改的解决方法
2021/04/16 SQL Server
java解析XML详解
2021/07/09 Java/Android