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的装饰器功能时,突然人 ...
随机推荐
- the core of Git is a simple key-value data store The objects directory stores all the content for your database
w https://git-scm.com/book/en/v1/Git-Internals-Plumbing-and-Porcelain Git is a content-addressable f ...
- Power Systems 虚拟化简介
本文向您详细地介绍了 Power System 虚拟化相关的技术和亮点,让您对这些最新的虚拟化技术有一个全面的了解.本文来自 IBM Systems Magazine for AIX 中文版. 自从引 ...
- Python Interpreter
在开始之前,我们先限定下python解释器的意思.当讨论Python的时候,解释器这个词可以用在不同的地方.有的时候,解释器指的是Python Interpreter,也就是你在命令行交互界面上输入p ...
- T-SQL利用笛卡尔积累计、累加
T-SQL利用笛卡尔积累计.累加 笛卡尔积 --原始数据 select templateid,needitem1Count from db_tank..TS_CardMain --累计数据 selec ...
- windows 和rhel,centos双系统安装
1:首先确保你先安装为windows系统,为indows7以上的把. 2:安装好为indows系统后,进入系统后把磁盘分区,分出足够的空间为安装linux. 3:再为windows下使用软碟通等工具制 ...
- 剑指offer 面试22题
面试22题: 题目:链表中倒数第k个节点 题:输入一个链表,输出该链表中倒数第k个结点. 解题思路:为了实现只遍历链表一次就能找到倒数第k个节点,我们可以定义两个指针.让第一个指针先向前走k-1步,第 ...
- selenium网页没加载完成就停止加载并自动刷新
判断一个网页10秒没加载完成就停止加载并自动刷新 driver=webdriver.Chome() driver.set_page_load_timeout(10) while True: try: ...
- 【转】Python爬虫_示例
爬虫项目:爬取汽车之家新闻资讯 # requests+Beautifulsoup爬取汽车之家新闻 import requests from bs4 import BeautifulSoup res ...
- Java基础教程:泛型基础
Java基础教程:泛型基础 引入泛型 传统编写的限制: 在Java中一般的类和方法,只能使用具体的类型,要么是基本数据类型,要么是自定义类型.如果要编写可以应用于多种类型的代码,这种刻板的限制就会束缚 ...
- Vimium~让您的Chrome起飞
工欲善其事,必先利其器!撸起Vimium,我的Chrome就这么起飞了. 学起(了解几个快捷键即可)And撸起Vimium,想黑客一般在Chrome上飞起.Vimium常用快捷键(注:区分大小写)j, ...