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的装饰器功能时,突然人 ...
随机推荐
- IlRuntime + protobuf-net
环境unity566,.net2.0 下载protobuf-net https://github.com/mgravell/protobuf-net/tree/r668 因为这个vs2015就可以打开 ...
- python——random模块
用法示例: import random # 1)随机小数 print(random.random()) # 获取大于0且小于1 之间的小数 random.random() print(random.u ...
- 我的Android进阶之旅------>HTTP Content-type 对照表
HTTP Content-type 对照表 文件扩展名 Content-Type(Mime-Type) 文件扩展名 Content-Type(Mime-Type) .*( 二进制流, 不知道下载文件类 ...
- boost之数据结构和容器
1.静态数组array,boost对静态数组进行了封装,使用和普通数组一样的初始化式进行初始化. #include <iostream> #include <boost/array. ...
- Open SQL和Native SQL到底有什么本质的区别
1.個人愚見:它們只是在实现的方式上,执行效率上不同,有的书上还说native sql存在一定风险 *& 20170521 171300 1.Open sql 是由创建数据库数据的ABAP命令 ...
- 我的第二个Python小程序
输出0-100之间的偶数: # Author: fansik # Description: Output an even number between 0 and 100 # method one n ...
- linux下增加swap分区
Swap交换分区概念 什么是Linux swap space呢?我们先来看看下面两段关于Linux swap space的英文介绍资料: Linux divides its physical RAM ...
- 剑指offer 面试31题
面试31题: 题目:栈的压入.弹出元素 题:输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈的压入顺序 ...
- ZOJ 3961 Let's Chat 【水】
题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3961 题意 给出两个人的发消息的记录,然后 如果有两人在连续M天 ...
- POJ 3468 A Simple Problem with Integers 【线段树】
题目链接 http://poj.org/problem?id=3468 思路 线段树 区间更新 模板题 在赋初始值的时候,按点更新区间就可以 AC代码 #include <cstdio> ...