参考网址:

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. 操作系统/应用程序、操作中的“并发”、线程和进程,python中线程和进程(GIL锁),python线程编写+锁

    并发编程前言: 1.网络应用 1)爬虫 直接应用并发编程: 2)网络框架 django flask tornado 源码-并发编程 3)socketserver 源码-并发编程 2.运维领域 1)自动 ...

  2. 剑指offer 面试42题

    面试42题: 题目:连续子数组的最大和 题:输入一个整形数组,数组里有正数也有负数.数组中的一个或连续多个整数组成一个子数组.求所有子数组的和的最大值.要求时间复杂度为O(n) 解题思路:在数组里从前 ...

  3. ORA-00001:unique constraint violated 以及 Incorrect result size: expected 1, actual 0

    往数据库中插入数据时报错:   www.2cto.com   ORA-00001: unique constraint (IDX_CARTON_HEADER)violated.   即往CARTON_ ...

  4. SpringMVC:学习笔记(5)——数据绑定及表单标签

    SpringMVC——数据绑定及表单标签 理解数据绑定 为什么要使用数据绑定 基于HTTP特性,所有的用户输入的请求参数类型都是String,比如下面表单: 按照我们以往所学,如果要获取请求的所有参数 ...

  5. 【CodeChef】Enormous Input Test

    The purpose of this problem is to verify whether the method you are using to read input data is suff ...

  6. 【FAQ系列】Relay log 导致复制启动失败

    今天在使用冷备份文件重做从库时遇到一个报错,值得研究一下. 版本:MySQL5.6.27 一.报错现象 dba:(none)> start slave; ERROR (HY000): Slave ...

  7. 【Flask】Sqlalchemy 外键

    ### 外键:使用SQLAlchemy创建外键非常简单.在从表中增加一个字段,指定这个字段外键的是哪个表的哪个字段就可以了.从表中外键的字段,必须和父表的主键字段类型保持一致.示例代码如下: from ...

  8. [POI2008]账本BBB

    题目 BZOJ 做法 明确: \(~~~1.\)为了达到目标分数所取反的次数是固定的 \(~~~2.\)为了满足前缀非负,得增加取反和滚动次数 滚动的次数可以枚举,增加的取反可以通过最小前缀和得到 滚 ...

  9. ucsc genome brower的用法和说明(一)

    官网说明书:http://genome.ucsc.edu/goldenpath/help/hgTracksHelp.html 1.genome brower的作用 a,展示任何尺度的基因组片段.比如, ...

  10. iOS_AutoLayout自动布局

    目录: 一.什么是AutoLayout? 二.创建autoLayout的方法 三.VFL语言     一.什么是AutoLayout? Autolayout是一种“自动布局”技术,专门用来布局UI界面 ...