Python新手学习装饰器


Posted in Python onJune 04, 2020

python函数式编程之装饰器

1.开放封闭原则

简单来说,就是对扩展开放,对修改封闭。

在面向对象的编程方式中,经常会定义各种函数。一个函数的使用分为定义阶段和使用阶段,一个函数定义完成以后,可能会在很多位置被调用。这意味着如果函数的定义阶段代码被修改,受到影响的地方就会有很多,此时很容易因为一个小地方的修改而影响整套系统的崩溃,所以对于现代程序开发行业来说,一套系统一旦上线,系统的源代码就一定不能够再改动了。然而一套系统上线以后,随着用户数量的不断增加,一定会为一套系统扩展添加新的功能。

此时,又不能修改原有系统的源代码,又要为原有系统开发增加新功能,这就是程序开发行业的开放封闭原则,这时就要用到装饰器了。

2.什么是装饰器

装饰器,顾名思义,就是装饰,修饰别的对象的一种工具。

所以装饰器可以是任意可调用的对象,被装饰的对象也可以是任意可调用对象。

3.装饰器的作用

在不修改被装饰对象的源代码以及调用方式的前提下为被装饰对象添加新功能。

原则:

1.不修改被装饰对象的源代码

2.不修改被装饰对象的调用方式

目标:

为被装饰对象添加新功能。

实例扩展:

import time
# 装饰器函数
def wrapper(func):
 def done(*args,**kwargs):
  start_time = time.time()
  func(*args,**kwargs)
  stop_time = time.time()
  print('the func run time is %s' % (stop_time - start_time))
 return done
# 被装饰函数1
@wrapper
def test1():
 time.sleep(1)
 print("in the test1")
# 被装饰函数2
@wrapper
def test2(name): #1.test2===>wrapper(test2) 2.test2(name)==dome(name)
 time.sleep(2)
 print("in the test2,the arg is %s"%name)
# 调用
test1()
test2("Hello World")

不含参数实例:

import time
user,passwd = 'admin','admin'
def auth(auth_type):
 print("auth func:",auth_type)
 def outer_wrapper(func):
  def wrapper(*args, **kwargs):
   print("wrapper func args:", *args, **kwargs)
   if auth_type == "local":
    username = input("Username:").strip()
    password = input("Password:").strip()
    if user == username and passwd == password:
     print("\033[32;1mUser has passed authentication\033[0m")
     res = func(*args, **kwargs) # from home
     print("---after authenticaion ")
     return res
    else:
     exit("\033[31;1mInvalid username or password\033[0m")
   elif auth_type == "ldap":
    print("ldap链接")
  return wrapper
 return outer_wrapper
@auth(auth_type="local") # home = wrapper()
def home():
 print("welcome to home page")
 return "from home"
@auth(auth_type="ldap")
def bbs():
 print("welcome to bbs page"
print(home()) #wrapper()
bbs()

到此这篇关于Python新手学习装饰器的文章就介绍到这了,更多相关Python之装饰器简介内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
centos下更新Python版本的步骤
Feb 12 Python
python基础教程之常用运算符
Aug 29 Python
python定时执行指定函数的方法
May 27 Python
Python中格式化format()方法详解
Apr 01 Python
Mac中Python 3环境下安装scrapy的方法教程
Oct 26 Python
Python sklearn KFold 生成交叉验证数据集的方法
Dec 11 Python
基于python实现把json数据转换成Excel表格
May 07 Python
python中pandas库中DataFrame对行和列的操作使用方法示例
Jun 14 Python
如何使用Python处理HDF格式数据及可视化问题
Jun 24 Python
Pytorch之Tensor和Numpy之间的转换的实现方法
Sep 03 Python
一行代码python实现文件共享服务器
Apr 22 Python
pytorch中的numel函数用法说明
May 13 Python
基于python 取余问题(%)详解
Jun 03 #Python
Python中关于logging模块的学习笔记
Jun 03 #Python
Python学习之os模块及用法
Jun 03 #Python
Python爬虫HTPP请求方法有哪些
Jun 03 #Python
什么是Python变量作用域
Jun 03 #Python
Python Flask框架实现简单加法工具过程解析
Jun 03 #Python
python自定义函数def的应用详解
Jun 03 #Python
You might like
IIS6+PHP5+MySQL5+Zend Optimizer+phpMyAdmin安装配置图文教程 2009年
2009/06/08 PHP
joomla内置的表单验证功能使用方法
2010/06/11 PHP
为IP查询添加GOOGLE地图功能的代码
2010/08/08 PHP
php网页标题中文乱码的有效解决方法
2014/03/05 PHP
PHP+AjaxForm异步带进度条上传文件实例代码
2017/08/14 PHP
PHP迭代与递归实现无限级分类
2017/08/28 PHP
让回调函数 showResponse 也带上参数的代码
2007/08/13 Javascript
只需20行代码就可以写出CSS覆盖率测试脚本
2013/04/24 Javascript
JavaScript实现的经典文件树菜单效果
2015/09/08 Javascript
javascript函数中的3个高级技巧
2016/09/22 Javascript
JS验证 只能输入小数点,数字,负数的实现方法
2016/10/07 Javascript
浅谈jquery上下滑动的注意事项
2016/10/13 Javascript
jQuery UI Draggable + Sortable 结合使用(实例讲解)
2017/09/07 jQuery
一个Vue视频媒体多段裁剪组件的实现示例
2018/08/09 Javascript
javascript实现超好看的3D烟花特效
2020/01/01 Javascript
python实现逆波兰计算表达式实例详解
2015/05/06 Python
全面理解Python中self的用法
2016/06/04 Python
如何将python中的List转化成dictionary
2016/08/15 Python
python解决Fedora解压zip时中文乱码的方法
2016/09/18 Python
利用Python实现Windows定时关机功能
2017/03/21 Python
tensorflow 加载部分变量的实例讲解
2018/07/27 Python
python实现给微信指定好友定时发送消息
2019/04/29 Python
使用Python实现毫秒级抢单功能
2019/06/06 Python
python中如何使用insert函数
2020/01/09 Python
python的reverse函数翻转结果为None的问题
2020/05/11 Python
TensorFlow固化模型的实现操作
2020/05/26 Python
python rolling regression. 使用 Python 实现滚动回归操作
2020/06/08 Python
canvas与html5实现视频截图功能示例
2016/12/15 HTML / CSS
销售人员自我评价怎么写
2013/09/19 职场文书
电钳专业个人求职信
2014/01/04 职场文书
租房安全协议书
2014/08/20 职场文书
2015年六一儿童节活动方案
2015/05/05 职场文书
教师节表彰会主持词
2015/07/06 职场文书
2016猴年春节问候语
2015/11/11 职场文书
SpringBoot读取Resource下文件的4种方法
2021/07/02 Java/Android
如何通过cmd 连接阿里云服务器
2022/04/18 Servers