Python装饰器用法实例分析


Posted in Python onJanuary 14, 2019

本文实例讲述了Python装饰器用法。分享给大家供大家参考,具体如下:

无参数的装饰器

#coding=utf-8
def log(func):
  def wrapper():
    print 'before calling ',func.__name__
    func()
    print 'end calling ',func.__name__
  return wrapper
@log
def hello():
  print 'hello'
@log
def hello2(name):
  print 'hello',name
if __name__=='__main__':
  hello()

运行结果:

before calling  hello
hello
end calling  hello

带参数的装饰器:

#coding=utf-8
def log(func):
  def wrapper(name):
    print 'before calling ',func.__name__
    func(name)
    print 'end calling ',func.__name__
  return wrapper
@log
def hello(name):
  print 'hello',name
@log
def hello2(name):
  print 'hello',name
if __name__=='__main__':
  hello('haha')

运行结果:

before calling  hello
hello haha
end calling  hello

多个参数的时候:

#coding=utf-8
def log(func):
  '''
  *无名字的参数
  **有名字的参数
  :param func:
  :return:
  '''
  def wrapper(*args,**kvargs):
    print 'before calling ',func.__name__
    print 'args',args,'kvargs',kvargs
    func(*args,**kvargs)
    print 'end calling ',func.__name__
  return wrapper
@log
def hello(name,age):
  print 'hello',name,age
@log
def hello2(name):
  print 'hello',name
if __name__=='__main__':
  hello('haha',2)
  hello(name='hehe',age=3)

输出:

end calling  hello
before calling  hello
args () kvargs {'age': 3, 'name': 'hehe'}
hello hehe 3
end calling  hello

装饰器里带参数的情况

本质就是嵌套函数

#coding=utf-8
def log(level,*args,**kvargs):
  def inner(func):
    def wrapper(*args,**kvargs):
      print level,'before calling ',func.__name__
      print level,'args',args,'kvargs',kvargs
      func(*args,**kvargs)
      print level,'end calling ',func.__name__
    return wrapper
  return inner
@log(level='INFO')
def hello(name,age):
  print 'hello',name,age
@log
def hello2(name):
  print 'hello',name
if __name__=='__main__':
  hello('haha',2)

运行输出:

INFO before calling  hello
INFO args ('haha', 2) kvargs {}
hello haha 2
INFO end calling  hello

更多关于Python相关内容可查看本站专题:《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
用Python给文本创立向量空间模型的教程
Apr 23 Python
全面了解python字符串和字典
Jul 07 Python
pandas 快速处理 date_time 日期格式方法
Nov 12 Python
python启动应用程序和终止应用程序的方法
Jun 28 Python
Python字典推导式将cookie字符串转化为字典解析
Aug 10 Python
Python Django框架模板渲染功能示例
Nov 08 Python
如何使用Python多线程测试并发漏洞
Dec 18 Python
python如何获取apk的packagename和activity
Jan 10 Python
Python守护进程实现过程详解
Feb 10 Python
keras使用Sequence类调用大规模数据集进行训练的实现
Jun 22 Python
python+appium+yaml移动端自动化测试框架实现详解
Nov 24 Python
Python自动化工具之实现Excel转Markdown表格
Apr 08 Python
浅谈python str.format与制表符\t关于中文对齐的细节问题
Jan 14 #Python
对Python中创建进程的两种方式以及进程池详解
Jan 14 #Python
对Python多线程读写文件加锁的实例详解
Jan 14 #Python
Python多进程写入同一文件的方法
Jan 14 #Python
python 将大文件切分为多个小文件的实例
Jan 14 #Python
使用k8s部署Django项目的方法步骤
Jan 14 #Python
Python数据可视化教程之Matplotlib实现各种图表实例
Jan 13 #Python
You might like
php使用exec shell命令注入的方法讲解
2013/11/12 PHP
ThinkPHP学习笔记(一)ThinkPHP部署
2014/06/22 PHP
PHP 断点续传实例详解
2017/11/11 PHP
php实现mysql连接池效果实现代码
2018/01/25 PHP
baidu博客的编辑友情链接的新的层窗口!经典~支持【FF】
2007/02/09 Javascript
jquery复选框CHECKBOX全选、反选
2008/08/30 Javascript
Javascript的各种节点操作实例演示代码
2012/06/27 Javascript
JavaScript高级程序设计 阅读笔记(十七) js事件
2012/08/14 Javascript
jQuery实现html表格动态添加新行的方法
2015/05/28 Javascript
学习JavaScript设计模式(多态)
2015/11/25 Javascript
AngularJs实现ng1.3+表单验证
2015/12/10 Javascript
关于JS 预解释的相关理解
2016/06/28 Javascript
Node.js connect ECONNREFUSED错误解决办法
2016/09/15 Javascript
bootstrap datetimepicker2.3.11时间插件使用
2016/11/19 Javascript
JQuery实现动态操作表格
2017/01/11 Javascript
一篇文章搞定JavaScript类型转换(面试常见)
2017/01/21 Javascript
vue2.0结合Element实现select动态控制input禁用实例
2017/05/12 Javascript
关于JS与jQuery中的文档加载问题
2017/08/22 jQuery
JS基于对象的特性实现去除数组中重复项功能详解
2017/11/17 Javascript
详解Angular操作cookies方法
2018/06/01 Javascript
使用vue重构资讯页面的实例代码解析
2019/11/26 Javascript
Vue 401配合Vuex防止多次弹框的案例
2020/11/11 Javascript
Python、Javascript中的闭包比较
2015/02/04 Python
python不换行之end=与逗号的意思及用途
2017/11/21 Python
Python实现变声器功能(萝莉音御姐音)
2019/12/05 Python
Python利用PyPDF2库获取PDF文件总页码实例
2020/04/03 Python
解决python运行启动报错问题
2020/06/01 Python
基于Python爬取京东双十一商品价格曲线
2020/10/23 Python
新加坡航空官方网站:Singapore Airlines
2016/10/13 全球购物
ECCO爱步官方旗舰店:丹麦鞋履品牌
2018/01/02 全球购物
加拿大在线旅游公司:Flighthub
2019/03/11 全球购物
乐高瑞士官方商店:LEGO CH
2020/08/16 全球购物
营业员演讲稿
2013/12/30 职场文书
质量安全标语
2014/06/07 职场文书
永远是春天观后感
2015/06/12 职场文书
2016年植树节红领巾广播稿
2015/12/17 职场文书