众所周知,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装饰器,如何更优雅的重用代码的更多相关文章

  1. Python中利用函数装饰器实现备忘功能

    Python中利用函数装饰器实现备忘功能 这篇文章主要介绍了Python中利用函数装饰器实现备忘功能,同时还降到了利用装饰器来检查函数的递归.确保参数传递的正确,需要的朋友可以参考下   " ...

  2. python 中多个装饰器的执行顺序

    python 中多个装饰器的执行顺序: def wrapper1(f1): print('in wrapper1') def inner1(*args,**kwargs): print('in inn ...

  3. 第7.26节 Python中的@property装饰器定义属性访问方法getter、setter、deleter 详解

    第7.26节 Python中的@property装饰器定义属性访问方法getter.setter.deleter 详解 一.    引言 Python中的装饰器在前面接触过,老猿还没有深入展开介绍装饰 ...

  4. Python进阶之decorator装饰器

    decorator装饰器 .note-content {font-family: "Helvetica Neue",Arial,"Hiragino Sans GB&quo ...

  5. python中面向对象之装饰器

    python面向对象内置装饰器property,staticmethod,classmethod的使用 @property 装饰器作用及使用 作用:面向对象中的方法伪装成属性 使用如下: class ...

  6. Python中的各种装饰器详解

    Python装饰器,分两部分,一是装饰器本身的定义,一是被装饰器对象的定义. 一.函数式装饰器:装饰器本身是一个函数. 1.装饰函数:被装饰对象是一个函数 [1]装饰器无参数: a.被装饰对象无参数: ...

  7. Python中的单例模式——装饰器实现剖析

    Python中单例模式的实现方法有多种,但在这些方法中属装饰器版本用的广,因为装饰器是基于面向切面编程思想来实现的,具有很高的解耦性和灵活性. 单例模式定义:具有该模式的类只能生成一个实例对象. 先将 ...

  8. python中闭包和装饰器的理解(关于python中闭包和装饰器解释最好的文章)

    转载:http://python.jobbole.com/81683/ 呵呵!作为一名教python的老师,我发现学生们基本上一开始很难搞定python的装饰器,也许因为装饰器确实很难懂.搞定装饰器需 ...

  9. Python中的@property装饰器

    要了解@property的用途,首先要了解如何创建一个属性. 一般而言,属性都通过__init__方法创建,比如: class Student(object): def __init__(self,n ...

随机推荐

  1. RxJava系列1(简介)

    RxJava系列1(简介) RxJava系列2(基本概念及使用介绍) RxJava系列3(转换操作符) RxJava系列4(过滤操作符) RxJava系列5(组合操作符) RxJava系列6(从微观角 ...

  2. POJ-3069 Saruman's Army---区间选点

    题目链接: https://vjudge.net/problem/POJ-3069 题目大意: 在一条直线上,有n个点.从这n个点中选择若干个,给他们加上标记.对于每一个点,其距离为R以内的区域里必须 ...

  3. echarts版本折线图

    1.效果如下:         绘制折线图,应该算是说echarts中使用最简单也算使用频率最高的一种功能了吧.根据官网列子能找出规律,只是有些属性对于初接触者来说,会有点陌生,不过仔细阅读一下还是不 ...

  4. MSSQL 复制数据 并随机打乱写入

    select * into temp from XX order by newid() -- 复制表结构 truncate table XX -- 清空表 SET IDENTITY_INSERT XX ...

  5. JS刷票神器

    var all = document.querySelectorAll('td[width="80"]'); setInterval(function(){ document.ge ...

  6. [LeetCode] Dota2 Senate 刀塔二参议院

    In the world of Dota2, there are two parties: the Radiant and the Dire. The Dota2 senate consists of ...

  7. [LeetCode] Maximum Vacation Days 最大化休假日

    LeetCode wants to give one of its best employees the option to travel among N cities to collect algo ...

  8. [LeetCode] Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之二

    Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...

  9. 树莓派控制高电平蜂鸣器(c语言+新手向)

    话不多说,先上代码: #include <wiringPi.h> #include <stdio.h> #include <sys/time.h> #define ...

  10. 报错pymysql.err.DataError: (1406, "Data too long for column 'gender' at row 1")

    在Django默认的admin后台创建超级用户时, 报错pymysql.err.DataError: (1406, "Data too long for column 'gender' at ...