python 各种装饰器示例(python3)
参考网址:
Python中的各种装饰器详解_python_脚本之家
http://www.jb51.net/article/63892.htm
一、函数式装饰器:
1、装饰器无参数,被装饰对象无参数
def test(func):
def _test(): #_test函数内使用或返回func()
print('call the function {0}.'.format(func.__name__))
return func()
return _test @test
def say():
print('hello,world') say() 输出:
call the function say.
hello,world
2、装饰器无参数,被装饰对象有参数
def test(func):
def _test(*args,**kw):
print('call the function {0}.'.format(func.__name__))
return func(*args,**kw)
return _test @test
def say(str1,length):
print(str1[:length]) say('hello,world',5) 输出:
call the function say.
hello
3、装饰器有参数,被装饰对象无参数
def arg_test(str_arg):
def test(func):
def _test():
print('call the function {0}.'.format(func.__name__))
print(str_arg)
return func()
return _test
return test @arg_test('with arg')
def say():
print('hello,world') say() 输出:
call the function say.
with arg
hello,world
如果装饰器有默认参数,则用@arg_test(),无参数的装饰器直接用@arg_test
4、装饰器有参数,被装饰对象有参数
def arg_test(str_arg):
def test(func):
def _test(*args,**kw):
print('call the function {0}.'.format(func.__name__))
print(str_arg)
return func(*args,**kw)
return _test
return test @arg_test('with arg')
def say(str1,length):
print(str1[:length]) say('hello,world',5) 输出:
call the function say.
with arg
hello
二、类装饰器
1、装饰器无参数,被装饰对象无参数
class test(object):
def __init__(self,func):
self._func = func def __call__(self):
print('call the function {0}. '.format(self._func.__name__))
return self._func() @test
def say():
print ('hello,world') @test
def hehe():
print('hehe') say()
hehe() 输出:
call the function say.
hello,world
call the function hehe.
hehe
2、装饰器无参数,被装饰对象有参数
class test(object):
def __init__(self,func):
self._func = func def __call__(self,*args,**kw):
print('call the function {0}. '.format(self._func.__name__))
return self._func(*args,**kw) @test
def say(str1,length):
print (str1[:length]) say('hello,world',5) 输出:
call the function say.
hello
3、装饰器有参数,被装饰对象无参数
class test(object):
def __init__(self,info='de_arg'):
self._info = info def __call__(self,func):
def __call():
print (self._info)
print('call the function {0}. '.format(func.__name__))
return func()
return __call @test()
def say():
print ('hello,world') say() 输出:
20 de_arg
21 call the function say.
22 hello,world
3、装饰器有参数,被装饰对象有参数
class test(object):
def __init__(self,info='de_arg'):
self._info = info def __call__(self,func):
def __call(*args,**kw):
print (self._info)
print('call the function {0}. '.format(func.__name__))
return func(*args,**kw)
return __call @test()
def say(str1,length):
print (str1[:length]) say('hello,world',5) 输出:
de_arg
call the function say.
hello
三、防止装饰器修改函数名 __name_-
# import functools def arg_test(str_arg):
def test(func):
# @functools.wraps(func)
def _test():
print('call the function {0}.'.format(func.__name__))
print(str_arg)
return func()
return _test
return test @arg_test('with arg')
def say():
print (say.__name__)
print('hello,world') say() 输出:
call the function say.
with arg
_test #函数名被修改
hello,world
使用functools包
1 import functools def arg_test(str_arg):
def test(func):
5 @functools.wraps(func)
def _test():
print('call the function {0}.'.format(func.__name__))
print(str_arg)
return func()
return _test
return test @arg_test('with arg')
def say():
print (say.__name__)
print('hello,world') say() 输出:
call the function say.
with arg
say #函数名不变
hello,world
python 各种装饰器示例(python3)的更多相关文章
- Python函数装饰器高级用法
在了解了Python函数装饰器基础知识和闭包之后,开始正式学习函数装饰器. 典型的函数装饰器 以下示例定义了一个装饰器,输出函数的运行时间: 函数装饰器和闭包紧密结合,入参func代表被装饰函数,通过 ...
- Python札记 -- 装饰器补充
本随笔是对Python札记 -- 装饰器的一些补充. 使用装饰器的时候,被装饰函数的一些属性会丢失,比如如下代码: #!/usr/bin/env python def deco(func): def ...
- 【转】详解Python的装饰器
原文链接:http://python.jobbole.com/86717/ Python中的装饰器是你进入Python大门的一道坎,不管你跨不跨过去它都在那里. 为什么需要装饰器 我们假设你的程序实现 ...
- python高级-装饰器(19)
一.什么是闭包 先看一个例子: #定义一个函数 def test(number): #在函数内部在定义一个函数,并且这个函数用到外围函数的变量 #那么将这个函数及用到的一些变量称之为闭包 def te ...
- 详解Python的装饰器
Python中的装饰器是你进入Python大门的一道坎,不管你跨不跨过去它都在那里. 为什么需要装饰器 我们假设你的程序实现了say_hello()和say_goodbye()两个函数. def sa ...
- 【转】【Python】装饰器
1.闭包 >>> def outer(): ... x = 1 ... def inner(): ... ... return inner >>> foo = ou ...
- 如何用python的装饰器定义一个像C++一样的强类型函数
Python作为一个动态的脚本语言,其函数在定义时是不需要指出参数的类型,也不需要指出函数是否有返回值.本文将介绍如何使用python的装饰器来定义一个像C++那样的强类型函数.接下去,先介绍 ...
- Python中装饰器(转)
本文由 伯乐在线 - 7even 翻译,艾凌风 校稿.未经许可,禁止转载!英文出处:Simeon Franklin.欢迎加入翻译组. 好吧,我标题党了.作为 Python 教师,我发现理解装饰器是学生 ...
- 【Python】装饰器实现日志记录
好的日志对一个软件的重要性是显而易见的.如果函数的入口都要写一行代码来记录日志,这种方式实在是太低效了,但一直没有找到更好的方法.后来用python写一些软件,了解到python的装饰器功能时,突然人 ...
随机推荐
- 实用的 集合工具类 和 String工具类
集合工具类:CollectionUtil method: 1.isNotEmpty() 不为空 2.isEmpty() 为空 举例:map集合 Map<String,String ...
- 使用jquery的ajax方法获取下拉列表值
AJAX 是一种用于创建快速动态网页的技术. 通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新.这意味着可以在不重新加载整个网页的情况下,对网页的某部分进行更新,用户体验非常好. ...
- 整理前端css/js/jq常见问题及解决方法(3)
jq: 1.prepend(参数);//将参数内容前置再某元素内部; eg: <div id="div1">奇妙能力歌</div> $("#div ...
- Django的models方法返回值异常,待解决
class BookInfo(models.Model): #创建书本信息类,继承models.Model booktitle=models.CharField(max_length=20) book ...
- Python——用正则求时间差
如有求时间差的需求,可直接套用此方法: import time true_time=time.mktime(time.strptime('2017-09-11 08:30:00','%Y-%m-%d ...
- 转:9个offer,12家公司,35场面试 从微软到谷歌,应届计算机毕业生的2012求职之路 !!!
1,简介 毕业答辩搞定,总算可以闲一段时间,把这段求职经历写出来,也作为之前三个半月的求职的回顾. 首先说说我拿到的offer情况: 微软,3面->终面,搞定 百度,3面->终面,口头of ...
- Java基础—运算符(转载)
转载自:Java运算符 计算机的最基本用途之一就是执行数学运算,作为一门计算机语言,Java也提供了一套丰富的运算符来操纵变量.我们可以把运算符分成以下几组: 算术运算符 关系运算符 位运算符 逻辑运 ...
- Python基础-常用的内置函数
内置函数filter str = ['a', 'b', 'c', 'd'] def fansik(num): if num != "a": return num ret = fil ...
- 简单springmvc在Eclipse的Tomcat上部署404error,直接在Tomcat上部署可以访问
在Eclipse工程下的.setting文件夹的org.eclipse.wst.common.component这个文件,如下则可以访问: <?xml version="1.0&quo ...
- 通过ip得到所在城市,以及城市所在经纬度坐标(监控系统中用的该代码,小航哥)
监控系统中就是利用的该段代码,实现通过ip得到所在城市,以及城市所在经纬度坐标,最后得以利用echarts实现模拟迁移的效果 api官方介绍: http://lbsyun.baidu.com/inde ...