浅析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 相关文章推荐
Django实现的自定义访问日志模块示例
Jun 23 Python
Python 获得13位unix时间戳的方法
Oct 20 Python
详解pyenv下使用python matplotlib模块的问题解决
Nov 29 Python
django 连接数据库 sqlite的例子
Aug 14 Python
Django shell调试models输出的SQL语句方法
Aug 29 Python
Python3多线程版TCP端口扫描器
Aug 31 Python
详解Django配置优化方法
Nov 18 Python
python psutil监控进程实例
Dec 17 Python
python实现超级马里奥
Mar 18 Python
pygame实现弹球游戏
Apr 14 Python
Python定时任务框架APScheduler原理及常用代码
Oct 05 Python
pdf论文中python画的图Type 3 fonts字体不兼容的解决方案
Apr 24 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中strlen()和mb_strlen()的区别浅析
2014/06/19 PHP
php设置页面超时时间解决方法
2015/09/22 PHP
PHP随手笔记整理之PHP脚本和JAVA连接mysql数据库
2015/11/25 PHP
List all the Databases on a SQL Server
2007/06/21 Javascript
Js 中debug方式
2010/02/07 Javascript
关于juqery radio写法的兼容性问题(新老版本jquery)
2010/06/14 Javascript
node.js中的events.EventEmitter.listenerCount方法使用说明
2014/12/08 Javascript
jquery实现的淡入淡出下拉菜单效果
2015/08/25 Javascript
JavaScript实现的多个图片广告交替显示效果代码
2015/09/04 Javascript
js匿名函数作为函数参数详解
2016/06/01 Javascript
Bootstrap Table的使用总结
2016/10/08 Javascript
微信小程序 wxapp内容组件 progress详细介绍
2016/10/31 Javascript
Bootstrap实现提示框和弹出框效果
2017/01/11 Javascript
vue学习笔记之vue1.0和vue2.0的区别介绍
2017/05/17 Javascript
jQuery自定义多选下拉框效果
2017/06/19 jQuery
webpack+vuex+axios 跨域请求数据的示例代码
2018/03/06 Javascript
小程序实现多列选择器
2019/02/15 Javascript
微信小程序实现点击空白隐藏的方法示例
2019/08/13 Javascript
关于JS模块化的知识点分享
2019/10/16 Javascript
js基于canvas实现时钟组件
2021/02/07 Javascript
Python的SQLalchemy模块连接与操作MySQL的基础示例
2016/07/11 Python
python 计算数组中每个数字出现多少次--“Bucket”桶的思想
2017/12/19 Python
Python面向对象程序设计中类的定义、实例化、封装及私有变量/方法详解
2019/02/28 Python
python基础梳理(一)(推荐)
2019/04/06 Python
Python列表操作方法详解
2020/02/09 Python
2020新版本pycharm+anaconda+opencv+pyqt环境配置学习笔记,亲测可用
2020/03/24 Python
Jupyter notebook设置背景主题,字体大小及自动补全代码的操作
2020/04/13 Python
PyCharm最新激活码(2020/10/27全网最新)
2020/10/27 Python
Champs Sports加拿大:北美最大的以商场为基础的专业运动鞋和服装零售商之一
2018/05/01 全球购物
师范学院教师自荐书
2014/01/31 职场文书
个人简历自我评价范文
2014/02/04 职场文书
销售顾问岗位职责
2014/02/25 职场文书
公司门卫工作职责
2014/06/28 职场文书
开学随笔
2015/08/15 职场文书
搞笑欢迎词大全
2015/09/30 职场文书
写作技巧:如何撰写一份优秀的营销策划书
2019/08/13 职场文书