浅析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删除windows垃圾文件的方法
Jul 14 Python
Python的Django框架中的数据过滤功能
Jul 17 Python
ubuntu系统下 python链接mysql数据库的方法
Jan 09 Python
python实现逻辑回归的方法示例
May 02 Python
Python数据结构与算法之链表定义与用法实例详解【单链表、循环链表】
Sep 28 Python
python数据抓取分析的示例代码(python + mongodb)
Dec 25 Python
对python添加模块路径的三种方法总结
Oct 16 Python
利用python脚本如何简化jar操作命令
Feb 24 Python
对Python中画图时候的线类型详解
Jul 07 Python
Django如何使用第三方服务发送电子邮件
Aug 14 Python
python调用函数、类和文件操作简单实例总结
Nov 29 Python
Python中itertools的用法详解
Feb 07 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中路径问题的解决方案
2006/10/09 PHP
php 中英文语言转换类
2011/09/07 PHP
PHP开发框架laravel安装与配置教程
2015/03/13 PHP
PHP识别二维码的方法(php-zbarcode安装与使用)
2016/07/07 PHP
浅谈PHP命令执行php文件需要注意的问题
2016/12/16 PHP
PHP实现的观察者模式实例
2017/06/21 PHP
thinkphp3.2同时连接两个数据库的简单方法
2019/08/13 PHP
js模拟点击事件实现代码
2012/11/06 Javascript
解决css和js的{}与smarty定界符冲突问题的两种方法
2013/09/10 Javascript
JavaScript eval() 函数介绍及应用示例
2014/07/29 Javascript
Nodejs中调用系统命令、Shell脚本和Python脚本的方法和实例
2015/01/01 NodeJs
Javascript中的arguments与重载介绍
2015/03/15 Javascript
JavaScript代码判断点击第几个按钮
2015/12/13 Javascript
利用css+原生js制作简单的钟表
2020/04/07 Javascript
JS获取填报扩展单元格控件的值的解决办法
2017/07/14 Javascript
使用vuex的state状态对象的5种方式
2018/04/19 Javascript
webpack4+Vue搭建自己的Vue-cli项目过程分享
2018/08/29 Javascript
使用Sonarqube扫描Javascript代码的示例
2018/12/26 Javascript
vue从零实现一个消息通知组件的方法详解
2020/03/16 Javascript
python BeautifulSoup使用方法详解
2013/11/21 Python
python实现根据图标提取分类应用程序实例
2014/09/28 Python
python获取本机mac地址和ip地址的方法
2015/04/29 Python
Python文件右键找不到IDLE打开项解决办法
2015/06/08 Python
python邮件发送smtplib使用详解
2020/06/16 Python
Python模拟登录的多种方法(四种)
2018/06/01 Python
浅谈Python3中strip()、lstrip()、rstrip()用法详解
2019/04/29 Python
使用Python制作表情包实现换脸功能
2019/07/19 Python
keras自定义回调函数查看训练的loss和accuracy方式
2020/05/23 Python
python如何遍历指定路径下所有文件(按按照时间区间检索)
2020/09/14 Python
基于PyInstaller各参数的含义说明
2021/03/04 Python
ghd官网:英国ghd直发器品牌
2018/05/04 全球购物
大学生活动总结模板
2014/07/02 职场文书
2015年污水处理厂工作总结
2015/05/26 职场文书
《平移和旋转》教学反思
2016/02/19 职场文书
机关单位2016年法制宣传日活动总结
2016/04/01 职场文书
公司年会晚会开幕词
2019/04/02 职场文书