参考网址:

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)的更多相关文章

  1. Python函数装饰器高级用法

    在了解了Python函数装饰器基础知识和闭包之后,开始正式学习函数装饰器. 典型的函数装饰器 以下示例定义了一个装饰器,输出函数的运行时间: 函数装饰器和闭包紧密结合,入参func代表被装饰函数,通过 ...

  2. Python札记 -- 装饰器补充

    本随笔是对Python札记 -- 装饰器的一些补充. 使用装饰器的时候,被装饰函数的一些属性会丢失,比如如下代码: #!/usr/bin/env python def deco(func): def ...

  3. 【转】详解Python的装饰器

    原文链接:http://python.jobbole.com/86717/ Python中的装饰器是你进入Python大门的一道坎,不管你跨不跨过去它都在那里. 为什么需要装饰器 我们假设你的程序实现 ...

  4. python高级-装饰器(19)

    一.什么是闭包 先看一个例子: #定义一个函数 def test(number): #在函数内部在定义一个函数,并且这个函数用到外围函数的变量 #那么将这个函数及用到的一些变量称之为闭包 def te ...

  5. 详解Python的装饰器

    Python中的装饰器是你进入Python大门的一道坎,不管你跨不跨过去它都在那里. 为什么需要装饰器 我们假设你的程序实现了say_hello()和say_goodbye()两个函数. def sa ...

  6. 【转】【Python】装饰器

    1.闭包 >>> def outer(): ... x = 1 ... def inner(): ... ... return inner >>> foo = ou ...

  7. 如何用python的装饰器定义一个像C++一样的强类型函数

        Python作为一个动态的脚本语言,其函数在定义时是不需要指出参数的类型,也不需要指出函数是否有返回值.本文将介绍如何使用python的装饰器来定义一个像C++那样的强类型函数.接下去,先介绍 ...

  8. Python中装饰器(转)

    本文由 伯乐在线 - 7even 翻译,艾凌风 校稿.未经许可,禁止转载!英文出处:Simeon Franklin.欢迎加入翻译组. 好吧,我标题党了.作为 Python 教师,我发现理解装饰器是学生 ...

  9. 【Python】装饰器实现日志记录

    好的日志对一个软件的重要性是显而易见的.如果函数的入口都要写一行代码来记录日志,这种方式实在是太低效了,但一直没有找到更好的方法.后来用python写一些软件,了解到python的装饰器功能时,突然人 ...

随机推荐

  1. MySql 安装常见问题汇总

    说明: 以下是针对 Mac 10.11 系统 以前,安装 MySql 数据库后, 设置的密码过于复杂,想更改为简单的密码, 方便数据库的使用. 1. 关闭和启动 MySql 数据库的方法: Syste ...

  2. 常用mongo语句

    只列出指定字段db.getCollection('PUBLICACCOUNTS').find({},{NickName:1,UserName:1,FID:1,_id:0})获取微信公众号列表db.ge ...

  3. Linux基础系列:常用命令(2)

    作业一: 1) 新建用户natasha,uid为1000,gid为555,备注信息为“master” groupadd -g 555 natasha useradd -u 1000 -g 555 -c ...

  4. SpringMVC:学习笔记(6)——转换器和格式化

    转换器和格式化 说明 SpringMVC的数据绑定并非没有限制,有案例表明,在SpringMVC如何正确绑定数据方面是杂乱无章的,比如在处理日期映射到Date对象上. 为了能够让SpringMVC进行 ...

  5. 深度学习3--caffe的安装(only CPU)

    1. 本来按照视频走的,但是在cmake的时候报错,然后参考了这篇文章,稀里糊涂的就好了,总结就是把“视频/本文”说的依赖都安装上,就可以了,先安装opencv,再安装caffe第三方依赖 在安装ca ...

  6. iOS 统计项目代码行数

    最近去面试 对面的"他" 问我其中一个问题 是 "你的项目代码量是多少?" 当时的确有点蒙圈, 我可以从整个项目打包的角度考虑项目大小,我还真没想过到底我的项目 ...

  7. solr、Lucene、IKAnalyzer这三者关系是怎样的?

    lucene 是开源搜索引擎 solr 是基于 lucene开发的搜索引擎 IK 是中文分词. lucene 不是一个搜索引擎,只是一个基础的文件索引工具包,或者叫“搜索引擎开发包”.不能单独作为程序 ...

  8. iOS获取某个日期后n个月的日期

    一.给一个时间,给一个数,正数是以后n个月,负数是前n个月: -(NSDate *)getPriousorLaterDateFromDate:(NSDate *)date withMonth:(NSI ...

  9. HTTP学习笔记01-URL

    URI URL语法 相对URL和绝对URL 相对URL URL的常用协议 http https mailto ftp rtsprtspu file news telnet 展望美好的未来 1.URI ...

  10. Django---Blog系统开发之建库

    数据库配置: #sqlite3数据库配置: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os. ...