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的反射机制
Jun 28 Python
asyncio 的 coroutine对象 与 Future对象使用指南
Sep 11 Python
分享一个简单的python读写文件脚本
Nov 25 Python
Python 实现使用dict 创建二维数据、DataFrame
Apr 13 Python
Python中的取模运算方法
Nov 10 Python
selenium+python截图不成功的解决方法
Jan 30 Python
Django框架中间件定义与使用方法案例分析
Nov 28 Python
django框架使用views.py的函数对表进行增删改查内容操作详解【models.py中表的创建、views.py中函数的使用,基于对象的跨表查询】
Dec 12 Python
Python autoescape标签用法解析
Jan 17 Python
python global和nonlocal用法解析
Feb 03 Python
Django使用channels + websocket打造在线聊天室
May 20 Python
Python开发简易五子棋小游戏
May 02 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文件上传实例详解!!!
2007/01/02 PHP
php分页原理 分页代码 分页类制作教程
2016/09/23 PHP
PHP实现求两个字符串最长公共子串的方法示例
2017/11/17 PHP
javascript dom 基本操作小结
2010/04/11 Javascript
jQuery计算textarea中文字数(剩余个数)的小程序
2013/11/28 Javascript
可编辑下拉框的2种实现方式
2014/06/13 Javascript
JavaScript极简入门教程(三):数组
2014/10/25 Javascript
node.js中的emitter.emit方法使用说明
2014/12/10 Javascript
如何编写高质量JS代码
2014/12/28 Javascript
js 获取元素在页面上的偏移量的方法汇总
2015/04/13 Javascript
JS实现的表格行鼠标点击高亮效果代码
2015/11/27 Javascript
Nodejs如何搭建Web服务器
2016/03/28 NodeJs
JS组件Bootstrap dropdown组件扩展hover事件
2016/04/17 Javascript
Angular2 http jsonp的实例详解
2017/08/31 Javascript
JS实现深度优先搜索求解两点间最短路径
2019/01/17 Javascript
Vue实现渲染数据后控制滚动条位置(推荐)
2019/12/09 Javascript
让你30分钟快速掌握vue3教程
2020/10/26 Javascript
Python实现MySQL操作的方法小结【安装,连接,增删改查等】
2017/07/12 Python
pandas DataFrame实现几列数据合并成为新的一列方法
2018/06/08 Python
python 读取视频,处理后,实时计算帧数fps的方法
2018/07/10 Python
Python Django切换MySQL数据库实例详解
2019/07/16 Python
适合Python初学者的一些编程技巧
2020/02/12 Python
Python tornado上传文件的功能
2020/03/26 Python
用python实现前向分词最大匹配算法的示例代码
2020/08/06 Python
Python+pyftpdlib实现局域网文件互传
2020/08/24 Python
matplotlib绘制鼠标的十字光标的实现(内置方式)
2021/01/06 Python
如何使用Python进行PDF图片识别OCR
2021/01/22 Python
使用纯 CSS 创作一个脉动 loader效果的源码
2018/09/28 HTML / CSS
英国时尚家具、家居饰品及礼品商店:Graham & Green
2016/09/15 全球购物
美国职棒大联盟的官方手套、球和头盔:Rawlings
2020/02/15 全球购物
违纪检讨书2000字
2014/02/08 职场文书
2014年教师党员公开承诺书
2014/05/28 职场文书
爱的承诺书
2015/01/20 职场文书
英语教师个人总结
2015/02/09 职场文书
中学生学习保证书
2015/02/26 职场文书
志愿者工作心得体会
2016/01/15 职场文书