详解Python装饰器


Posted in Python onMarch 25, 2019

1. 定义

本质是函数,用来装饰其他函数,为其他函数添加附加功能

2. 原则

a. 不能修改被装饰函数的源代码
b. 不能修改被装饰的函数的调用方式

3. 实现装饰器知识储备

a. 函数就是变量
b. 高阶函数
    i. 把一个函数当作实参传给另外一个函数,在不修改被装饰函数源代码情况下为其添加功能
    ii. 返回值中包含函数名, 不修改函数的调用方式
c. 嵌套函数
 高阶函数+嵌套函数==》装饰器

# Author: Lockegogo

user, passwd = 'LK', '130914'
def auth(auth_type):
 print('auth func:', auth_type)
 def outher_wrapper(func):
  def wrapper(*args, **kwargs):
   print('wrapper func:', *args, **kwargs)
   if auth_type == 'local':
    username = input('username:').strip()
    password = input('password:').strip()
    if user == username and password == passwd:
     print('\033[32;1mUser has passed authentication\033[0m')
     res = func(*args, **kwargs)
     return res
    else:
     exit('\033[32;1mInvalid Username or password\033[0m')
   elif auth_type == 'ldap':
    print('ldap,不会')
  return wrapper
 return outher_wrapper

def index():
 print('welcome to index page')
@auth(auth_type='local') # home = outher_wrapper(home)
def home():
 print('welcome to home page')
 return 'from home'
@auth(auth_type='ldap')
def bbs():
 print('welcome to bbs page')

index()
print(home())
bbs()

Decorator

以上所述是小编给大家介绍的Python装饰器详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Python 相关文章推荐
使用python 获取进程pid号的方法
Mar 10 Python
python安装以及IDE的配置教程
Apr 29 Python
Python可变参数用法实例分析
Apr 02 Python
Python使用matplotlib绘制正弦和余弦曲线的方法示例
Jan 06 Python
Python使用 Beanstalkd 做异步任务处理的方法
Apr 24 Python
Python 爬虫之Beautiful Soup模块使用指南
Jul 05 Python
在pycharm中设置显示行数的方法
Jan 16 Python
使用python爬取抖音视频列表信息
Jul 15 Python
Python Django Cookie 简单用法解析
Aug 13 Python
Python log模块logging记录打印用法解析
Jan 20 Python
Python3+Appium安装及Appium模拟微信登录方法详解
Feb 16 Python
Python趣味爬虫之用Python实现智慧校园一键评教
May 28 Python
详解用python自制微信机器人,定时发送天气预报
Mar 25 #Python
Python3.5实现的三级菜单功能示例
Mar 25 #Python
使用Django简单编写一个XSS平台的方法步骤
Mar 25 #Python
Python for循环与range函数的使用详解
Mar 23 #Python
详解Python读取yaml文件多层菜单
Mar 23 #Python
详解Python数据分析--Pandas知识点
Mar 23 #Python
详解Python基础random模块随机数的生成
Mar 23 #Python
You might like
php 无法加载mysql的module的时候的配置的解决方案引发的思考
2012/01/27 PHP
PHP实现根据图片色界在不同位置加水印的方法
2015/08/08 PHP
php 利用array_slice函数获取随机数组或前几条数据
2015/09/30 PHP
基于PHP如何把汉字转化为拼音
2015/12/11 PHP
Laravel 中使用简单的方法跟踪用户是否在线(推荐)
2019/10/30 PHP
JavaScript中的事件处理
2008/01/16 Javascript
javascript if条件判断方法小结
2014/05/17 Javascript
用js一次改变多个input的readonly属性值的方法
2014/06/11 Javascript
基于socket.io和node.js搭建即时通信系统
2014/07/30 Javascript
浅谈 javascript 事件处理
2015/01/04 Javascript
jquery实现适用于门户站的导航下拉菜单效果代码
2015/08/24 Javascript
探究Javascript模板引擎mustache.js使用方法
2016/01/26 Javascript
javascript中的3种继承实现方法
2016/01/27 Javascript
网页中JS函数自动执行常用三种方法
2016/03/30 Javascript
jquery遍历table的tr获取td的值实现方法
2016/05/19 Javascript
PhotoSwipe异步动态加载图片方法
2016/08/25 Javascript
Bootstrap Table 删除和批量删除
2017/09/22 Javascript
jQuery实现验证表单密码一致性及正则表达式验证邮箱、手机号的方法
2017/12/05 jQuery
小程序实现留言板
2018/11/02 Javascript
微信小程序实现写入读取缓存详解
2019/08/30 Javascript
python数字图像处理之高级滤波代码详解
2017/11/23 Python
python在线编译器的简单原理及简单实现代码
2018/02/02 Python
Python实现常见的回文字符串算法
2018/11/14 Python
Python3.5 Pandas模块之Series用法实例分析
2019/04/23 Python
Python continue语句实例用法
2020/02/06 Python
浅谈python print(xx, flush = True) 全网最清晰的解释
2020/02/21 Python
Django mysqlclient安装和使用详解
2020/09/17 Python
公司员工的自我评价范例
2013/11/01 职场文书
终端业务员岗位职责
2013/11/27 职场文书
销售人员获奖感言
2014/02/05 职场文书
爱国卫生月活动总结范文
2014/04/25 职场文书
中学生教师节演讲稿
2014/09/03 职场文书
2015年档案室工作总结
2015/05/23 职场文书
PyTorch 如何检查模型梯度是否可导
2021/06/05 Python
Python字符串格式化方式
2022/04/07 Python
Python四款GUI图形界面库介绍
2022/06/05 Python