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中针对函数处理的特殊方法
Mar 06 Python
Python程序员开发中常犯的10个错误
Jul 07 Python
简单介绍使用Python解析并修改XML文档的方法
Oct 15 Python
Python只用40行代码编写的计算器实例
May 10 Python
Python基于OpenCV实现视频的人脸检测
Jan 23 Python
浅谈pycharm的xmx和xms设置方法
Dec 03 Python
python实现自动获取IP并发送到邮箱
Dec 26 Python
Python实现爬取马云的微博功能示例
Feb 16 Python
详解Python基础random模块随机数的生成
Mar 23 Python
TensorFlow实现指数衰减学习率的方法
Feb 05 Python
python GUI库图形界面开发之PyQt5窗口布局控件QStackedWidget详细使用方法
Feb 27 Python
Python csv文件记录流程代码解析
Jul 16 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文件打开、关闭、写入的判断与执行代码
2011/05/24 PHP
php精确的统计在线人数的方法
2015/10/21 PHP
网页设计常用的一些技巧
2006/12/22 Javascript
Javascript常考语句107条收集
2010/03/09 Javascript
再谈javascript图片预加载技术(详细演示)
2011/03/12 Javascript
jquery 漂亮的删除确认和提交无刷新删除示例
2013/11/13 Javascript
开源的javascript项目Kissy介绍
2014/11/28 Javascript
举例详解AngularJS中ngShow和ngHide的使用方法
2015/06/19 Javascript
基于jQuery实现动态数字展示效果
2015/08/12 Javascript
基于React.js实现原生js拖拽效果引发的思考
2016/03/30 Javascript
Bootstrap登陆注册页面开发教程
2016/07/12 Javascript
require、backbone等重构手机图片查看器
2016/11/17 Javascript
使用AngularJS 跨站请求如何解决jsonp请求问题
2017/01/16 Javascript
Bootstrap表单制作代码
2017/03/17 Javascript
bootstrap响应式表格实例详解
2017/05/15 Javascript
JavaScript异步上传图片文件的实例代码
2017/07/04 Javascript
angularjs 缓存的使用详解
2018/03/19 Javascript
webpack+vue-cli项目中引入外部非模块格式js的方法
2018/09/28 Javascript
p5.js临摹旋转爱心
2019/10/23 Javascript
Vue中引入svg图标的两种方式
2021/01/14 Vue.js
深入讲解Java编程中类的生命周期
2016/02/05 Python
python3.7.0的安装步骤
2018/08/27 Python
python实现蒙特卡罗方法教程
2019/01/28 Python
python 处理telnet返回的More,以及get想要的那个参数方法
2019/02/14 Python
Python小程序之在图片上加入数字的代码
2019/11/26 Python
Python脚本实现监听服务器的思路代码详解
2020/05/28 Python
英格兰橄榄球商店:England Rugby Store
2016/12/17 全球购物
Nike意大利官网:Nike.com IT
2020/01/19 全球购物
如何进行有效的自我评价
2013/09/27 职场文书
客服实习的个人自我鉴定
2013/10/20 职场文书
报关报检委托书
2014/04/08 职场文书
优秀学生干部先进事迹材料
2014/05/26 职场文书
党校学习个人总结
2015/02/15 职场文书
2015年端午节活动策划书
2015/05/05 职场文书
军训心得体会范文(2016最新篇)
2016/01/11 职场文书
Python sklearn分类决策树方法详解
2022/09/23 Python