浅析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中的__SLOTS__属性使用示例
Feb 18 Python
python中将函数赋值给变量时需要注意的一些问题
Aug 18 Python
python音频处理用到的操作的示例代码
Oct 27 Python
python的pandas工具包,保存.csv文件时不要表头的实例
Jun 14 Python
python实现旋转和水平翻转的方法
Oct 25 Python
python实现二维插值的三维显示
Dec 17 Python
Django缓存系统实现过程解析
Aug 02 Python
python的sys.path模块路径添加方式
Mar 09 Python
Python非单向递归函数如何返回全部结果
Dec 18 Python
聊一聊python常用的编程模块
May 14 Python
浅谈Python响应式类库RxPy
Jun 14 Python
python对文档中元素删除,替换操作
Apr 02 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
ajax+php打造进度条代码[readyState各状态说明]
2010/04/12 PHP
PHP注释语法规范与命名规范详解篇
2018/01/21 PHP
PHP函数按引用传递参数及函数可选参数用法示例
2018/06/04 PHP
PHP单例模式模拟Java Bean实现方法示例
2018/12/07 PHP
在laravel中实现ORM模型使用第二个数据库设置
2019/10/24 PHP
页面中body onload 和 window.onload 冲突的问题的解决
2009/07/01 Javascript
Javascript this指针
2009/07/30 Javascript
jquery ajax 同步异步的执行示例代码
2010/06/23 Javascript
jQuery 下拉列表 二级联动插件分享
2012/03/29 Javascript
jQuery插件的写法分享
2013/06/12 Javascript
JS小功能(操作Table--动态添加删除表格及数据)实现代码
2013/11/28 Javascript
jQuery根据元素值删除数组元素的方法
2015/06/24 Javascript
深入浅析react native es6语法
2015/12/09 Javascript
js html css实现复选框全选与反选
2016/10/09 Javascript
JavaScript实现多叉树的递归遍历和非递归遍历算法操作示例
2018/02/08 Javascript
vue2.0 路由不显示router-view的解决方法
2018/03/06 Javascript
node.js实现微信开发之获取用户授权
2019/03/18 Javascript
[01:09:10]NB vs Liquid Supermajor小组赛 A组胜者组决赛 BO3 第一场 6.2
2018/06/04 DOTA
从零学Python之hello world
2014/05/21 Python
Python实现把xml或xsl转换为html格式
2015/04/08 Python
Python中使用ElementTree解析XML示例
2015/06/02 Python
python常见排序算法基础教程
2017/04/13 Python
老生常谈进程线程协程那些事儿
2017/07/24 Python
Python3.5内置模块之os模块、sys模块、shutil模块用法实例分析
2019/04/27 Python
python读取Excel表格文件的方法
2019/09/02 Python
关于Tensorflow使用CPU报错的解决方式
2020/02/05 Python
纯CSS3制作的鼠标悬停时边框旋转
2017/01/03 HTML / CSS
Html5中的桌面通知Notification的实现
2018/09/25 HTML / CSS
求∏的近似值,直到最后一项的绝对值小于指定的数
2016/02/12 面试题
EJB3推出JPA的原因
2013/10/16 面试题
电子商务应届生自我鉴定
2014/01/13 职场文书
职位说明书范文
2014/05/07 职场文书
推广活动策划方案
2014/08/23 职场文书
2014年客服工作总结与计划
2014/12/09 职场文书
Vue+Element UI实现概要小弹窗的全过程
2021/05/30 Vue.js
使用vue判断当前环境是安卓还是IOS
2022/04/12 Vue.js