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实现代码行数统计示例分享
Feb 10 Python
Django中使用group_by的方法
May 26 Python
Python的mysql数据库的更新如何实现
Jul 31 Python
python2.7读取文件夹下所有文件名称及内容的方法
Feb 24 Python
python 中if else 语句的作用及示例代码
Mar 05 Python
使用python实现抓取腾讯视频所有电影的爬虫
Apr 15 Python
python格式化输出保留2位小数的实现方法
Jul 02 Python
10款最好的Python开发编辑器
Jul 03 Python
在django中,关于session的通用设置方法
Aug 06 Python
python paramiko远程服务器终端操作过程解析
Dec 14 Python
python内打印变量之%和f的实例
Feb 19 Python
python 多线程共享全局变量的优劣
Sep 24 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中ADODB类详解
2008/03/25 PHP
为你总结一些php信息函数
2015/10/21 PHP
JSQL 批量图片切换的实现代码
2010/05/05 Javascript
基于jQuery的history历史记录插件
2010/12/11 Javascript
利用window.name实现windowStorage代码分享
2014/01/02 Javascript
JavaScript中String.prototype用法实例
2015/05/20 Javascript
js显示动态时间的方法详解
2016/08/20 Javascript
bootstrap flask登录页面编写实例
2016/11/01 Javascript
基于Angular.js实现的触摸滑动动画实例代码
2017/02/19 Javascript
JS运动改变单物体透明度的方法分析
2018/01/23 Javascript
JS数组去重常用方法实例小结【4种方法】
2018/05/28 Javascript
详解VUE中常用的几种import(模块、文件)引入方式
2018/07/03 Javascript
对Vue- 动态元素属性及v-bind和v-model的区别详解
2018/08/27 Javascript
js canvas实现写字动画效果
2018/11/30 Javascript
vue-列表下详情的展开与折叠案例
2020/07/28 Javascript
vue使用svg文件补充-svg放大缩小操作(使用d3.js)
2020/09/22 Javascript
Python中logging模块的用法实例
2014/09/29 Python
Pyhton中防止SQL注入的方法
2015/02/05 Python
在Python的Flask框架中实现全文搜索功能
2015/04/20 Python
详解Python的Django框架中的templates设置
2015/05/11 Python
Python注释详解
2016/06/01 Python
Python实现简单过滤文本段的方法
2017/05/24 Python
老生常谈Python基础之字符编码
2017/06/14 Python
pytorch torch.expand和torch.repeat的区别详解
2019/11/05 Python
Python解析微信dat文件的方法
2020/11/30 Python
python 实现百度网盘非会员上传超过500个文件的方法
2021/01/07 Python
python 制作网站筛选工具(附源码)
2021/01/21 Python
Groupon西班牙官方网站:在线优惠券和交易,节省高达70%
2021/03/13 全球购物
工商管理实习自我鉴定
2013/09/28 职场文书
三年级评语大全
2014/04/23 职场文书
党务公开方案
2014/05/06 职场文书
民主生活会对照检查材料思想汇报
2014/09/27 职场文书
导游欢送词
2015/01/31 职场文书
《画家和牧童》教学反思
2016/02/17 职场文书
升职感谢领导的话语及升职感谢信
2019/06/24 职场文书
SQL Server数据库的三种创建方法汇总
2023/05/08 MySQL