谈谈Python中的decorator装饰器,如何更优雅的重用代码
众所周知,Python本身有很多优雅的语法,让你能用一行代码写出其他语言很多行代码才能做的事情,比如:
最常用的迭代(eg: for i in range(1,10)), 列表生成式(eg: [ x*x for x in range(1,10) if x % 2 == 0])
map()能让你把函数作用于多个元素, reduce()能让你把多个元素的结果按照你预想的方式组合在一起,filter()能让你快速筛选出复合条件的数据
以上具体用法可以参考https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014317793224211f408912d9c04f2eac4d2af0d5d3d7b2000
而我们这次要讨论的装饰器decorator,可以在不改变现有函数的前提下更有效率的重用代码
比如我们在实际工作当中,经常需要添加try...except来捕获异常,但是一个个加也太麻烦了,此时我们就可以用decorator装饰器来实现
比如我们有以下原始函数
def hello():
print("Hello, world!") def bye():
print("Bye, world!")
正常情况下,如果都需要捕获异常的话,需要加两次try...except来做:
def main():
try:
hello()
except Exception as e:
print('except:', e)
....
....
try:
bye()
except Exception as e:
print('except:', e)
....
....
但是当我们有装饰器decorator的时候,一切都会变得特别优雅而简单,首先定义好我们的装饰器:
import functools def decorator_try(func):
@functools.wraps(func)
def wrapper(*arg,**kw):
try:
func(*arg, **kw)
except Exception as e:
print('except:', e)
return wrapper
然后只需要在原来的hello()和bye()函数定义之前添加一行语法就可以:
@decorator_try
def hello():
print("Hello, world!") @decorator_try
def bye():
print("Bye, world!")
然后执行的时候任何东西都不用加
def main():
hello()
....
....
bye()
结果为:
>>> hello()
Hello, world!
>>> hello(1,2)
except: hello() takes 0 positional arguments but 2 were given
具体的关于decorator装饰器的语法解释可以参考https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014318435599930270c0381a3b44db991cd6d858064ac0000
另外要注意装饰器定义中 func(*arg, **kw) 和 return wrapper的区别,注意看一个是带参数,一个不带参数与括号,带参数表示执行这个函数,不带参数和括号代表把定义的函数作为一个参数传递了过去,这对理解decorator的语法是至关重要的。因为:
把@decorator_try放到hello()函数的定义前,相当于执行了语句:
hello = decorator_try(hello)
如果想更深入的了解decorator装饰器,推荐一篇博文https://www.cnblogs.com/zh605929205/p/7704902.html
下面再写一个例子:
比如我们有一个函数,下载图片,用装饰器实现timeout之后,自动重新下载一次。
import functools
import random # 定义当timeout发生时要抛出的异常
class TimeOutError(Exception):
pass # 定义装饰器
def retry(func):
@functools.wrap(func)
def wrapper(*arg,**kwarg):
try:
print("first try...")
func(*arg,**kwarg)
except TimeOutError:
print("timeout occurs, retrying...")
func(*arg,**kwarg)
return wrapper # 定义download函数
@retry
def download():
print("downloading the photos...")
download_time = random.ranint(1,2)
if download_time>1:
print("the download_time > 1s, time out")
raise TimeOutError
else:
print("download finished.")
如果我们想要带参数的装饰器,则需要再多加一层函数嵌套:
#!/usr/bin/python
#-*- coding:utf-8 -*- import functools
import random # 定义当timeout发生时要抛出的异常
class TimeOutError(Exception):
pass # 定义装饰器
def decorator_download(text):
def decorator(func):
@functools.wraps(func)
def wrapper(*arg,**kwarg):
#try:
# print("first try...")
# func(*arg,**kwarg)
#except TimeOutError:
# print("timeout occurs, retrying...")
# func(*arg,**kwarg)
print(text)
print("first try...")
result = func(*arg,**kwarg)
while result == False:
print("will retry...")
result = func(*arg,**kwarg)
return wrapper
return decorator # 定义download函数
@decorator_download("retry until download finished successfully")
def download():
print("downloading the photos...")
download_time = random.randint(1,2)
if download_time>1:
print("the download_time > 1s, time out")
#raise TimeOutError
return False
else:
print("download finished.")
return True if __name__ == "__main__":
download()
谈谈Python中的decorator装饰器,如何更优雅的重用代码的更多相关文章
- Python中利用函数装饰器实现备忘功能
Python中利用函数装饰器实现备忘功能 这篇文章主要介绍了Python中利用函数装饰器实现备忘功能,同时还降到了利用装饰器来检查函数的递归.确保参数传递的正确,需要的朋友可以参考下 " ...
- python 中多个装饰器的执行顺序
python 中多个装饰器的执行顺序: def wrapper1(f1): print('in wrapper1') def inner1(*args,**kwargs): print('in inn ...
- 第7.26节 Python中的@property装饰器定义属性访问方法getter、setter、deleter 详解
第7.26节 Python中的@property装饰器定义属性访问方法getter.setter.deleter 详解 一. 引言 Python中的装饰器在前面接触过,老猿还没有深入展开介绍装饰 ...
- Python进阶之decorator装饰器
decorator装饰器 .note-content {font-family: "Helvetica Neue",Arial,"Hiragino Sans GB&quo ...
- python中面向对象之装饰器
python面向对象内置装饰器property,staticmethod,classmethod的使用 @property 装饰器作用及使用 作用:面向对象中的方法伪装成属性 使用如下: class ...
- Python中的各种装饰器详解
Python装饰器,分两部分,一是装饰器本身的定义,一是被装饰器对象的定义. 一.函数式装饰器:装饰器本身是一个函数. 1.装饰函数:被装饰对象是一个函数 [1]装饰器无参数: a.被装饰对象无参数: ...
- Python中的单例模式——装饰器实现剖析
Python中单例模式的实现方法有多种,但在这些方法中属装饰器版本用的广,因为装饰器是基于面向切面编程思想来实现的,具有很高的解耦性和灵活性. 单例模式定义:具有该模式的类只能生成一个实例对象. 先将 ...
- python中闭包和装饰器的理解(关于python中闭包和装饰器解释最好的文章)
转载:http://python.jobbole.com/81683/ 呵呵!作为一名教python的老师,我发现学生们基本上一开始很难搞定python的装饰器,也许因为装饰器确实很难懂.搞定装饰器需 ...
- Python中的@property装饰器
要了解@property的用途,首先要了解如何创建一个属性. 一般而言,属性都通过__init__方法创建,比如: class Student(object): def __init__(self,n ...
随机推荐
- Python内置函数(52)——getattr
英文文档: getattr(object, name[, default]) Return the value of the named attribute of object. name must ...
- [扩展推荐] —— Laravel Log 增强
Laravel Log Enhancer 是 Laravel 5.6 的一个扩展包,可以在 Laravel 日志中添加额外的数据. 得益于 Laravel 5.6 中日志的更新,这个包利用这些特性扩 ...
- guava-19.0和google-collections-1.0 的 ImmutableSet 类冲突
guava-19.0 google-collections-1.0 都有 ImmutableSet 类,包路径也一致,前者有 copyOf(Collection)? 一.应用报错: 二.解决办法 co ...
- MySql入门(2-2)创建数据库
mysql -u root -p; show databases; create database apigateway; use apigateway; show tables;
- 05_Linux目录文件操作命令2_我的Linux之路
这一节我们继续来学习Linux中对文件和目录的操作命令 mkdir 创建目录 mkdir (选项)(参数) 在Linux端可以使用mkdir来创建目录,如果你没有加其他的路径名,那么默认是在当前目录下 ...
- mysql 查询select语句汇总
数据准备: 创建表: create table students( id int unsigned primary key auto_increment not null, name varchar( ...
- Java-NIO(五):通道(Channel)的数据传输与内存映射文件
通道(Channel)的数据传输(采用非直接缓冲区) @Test public void testChannel() throws IOException { FileInputStream file ...
- YII2框架下使用PHPExcel导出柱状图
导出结果: 首先,到官网下载PHPExcel插件包,下载后文件夹如下: 将Classes文件夹放入到项目公共方法内. 新建控制器(访问导出的方法):EntryandexitController < ...
- C++ namespace的作用
namespace:命名空间或者叫名字空间,传统的c++只有一个全局的namespace,但是由于现在的程序规模越来越大,程序的分工越来越细,全局作用域就变得越来越拥挤,每个人都可能使用相同的名字来实 ...
- Now trying to drop the old temporary tablespace, the session hangs.
1.描述 问题描述:删除临时表空间时,会话Hangs挂起 SQL> drop tablespace TEMP_B including contents and datafiles; 2.故障诊断 ...