浅析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(Tornado)模拟登录小米抢手机
Nov 12 Python
pandas.DataFrame 根据条件新建列并赋值的方法
Apr 08 Python
详解Django 中是否使用时区的区别
Jun 14 Python
python的turtle库使用详解
May 10 Python
python爬虫 urllib模块反爬虫机制UA详解
Aug 20 Python
python使用正则来处理各种匹配问题
Dec 22 Python
Python os模块常用方法和属性总结
Feb 20 Python
python实现的分层随机抽样案例
Feb 25 Python
Pycharm最常用的快捷键及使用技巧
Mar 05 Python
pandas中ix的使用详细讲解
Mar 09 Python
通过Python实现一个简单的html页面
May 16 Python
Python函数参数定义及传递方式解析
Jun 10 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
造势之举?韩国总统候选人发布《星际争霸》地图
2017/04/22 星际争霸
基于Snoopy的PHP近似完美获取网站编码的代码
2011/10/23 PHP
PHP中仿制 ecshop验证码实例
2017/01/06 PHP
PHP编程获取各个时间段具体时间的方法
2017/05/26 PHP
php 算法之实现相对路径的实例
2017/10/17 PHP
php在windows环境下获得cpu内存实时使用率(推荐)
2018/02/08 PHP
SUN的《AJAX与J2EE》全文译了
2007/02/23 Javascript
Javascript 篱式条件判断
2008/08/22 Javascript
jQuery取得设置清空select选择的文本与值
2014/07/08 Javascript
Angular中$compile源码分析
2016/01/28 Javascript
一个超简单的jQuery回调函数例子(分享)
2016/08/08 Javascript
JSON与XML的区别对比及案例应用
2016/11/11 Javascript
web打印小结
2017/01/11 Javascript
js实现随机点名小功能
2017/08/17 Javascript
Webpack path与publicPath的区别详解
2018/05/03 Javascript
详解webpack4升级指南以及从webpack3.x迁移
2018/06/12 Javascript
vuejs使用axios异步访问时用get和post的实例讲解
2018/08/09 Javascript
vue-cli安装使用流程步骤详解
2018/11/08 Javascript
Vue.js中该如何自己维护路由跳转记录
2019/05/19 Javascript
Layui多选只有最后一个值的解决方法
2019/09/02 Javascript
jQuery实现数字华容道小游戏(实例代码)
2020/01/16 jQuery
JavaScript命令模式原理与用法实例详解
2020/03/10 Javascript
利用Python3分析sitemap.xml并抓取导出全站链接详解
2017/07/04 Python
Python pycharm 同时加载多个项目的方法
2019/01/17 Python
python利用Tesseract识别验证码的方法示例
2019/01/21 Python
浅谈Python基础—判断和循环
2019/03/22 Python
python实现的爬取电影下载链接功能示例
2019/08/26 Python
python lambda的使用详解
2021/02/26 Python
H5仿微信界面教程(一)
2017/07/05 HTML / CSS
HTML5 Canvas 绘图——使用 Canvas 绘制图形图文教程 使用html5 canvas 绘制精美的图
2015/08/31 HTML / CSS
财务管理专业自荐信范文
2013/12/24 职场文书
2014银行领导班子四风对照检查材料思想汇报
2014/09/25 职场文书
个人学习党的群众路线教育实践活动心得体会
2014/11/05 职场文书
2014年乡镇安全生产工作总结
2014/12/02 职场文书
建筑安全员岗位职责
2015/02/15 职场文书
高中升旗仪式主持词
2015/07/03 职场文书