众所周知,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. Python内置函数(21)——tuple

    英文文档: The constructor builds a tuple whose items are the same and in the same order as iterable's it ...

  2. vue项目中的常见问题

    总结了几个vue项目开发过程中遇到的常见问题,希望大家注意. 注:文末有福利! 一.样式问题 1.vue中使用less 安装less依赖 npm install less less-loader -- ...

  3. 图数据库orientDB(1-2)例子

    http://gog.orientdb.com/index.html#/infotab 小朱25岁,出生在教师家庭并且有个姐姐小田,他现在奋斗在帝都.  那么SQL是这样滴!!! CREATE VER ...

  4. idea的spring boot项目,运行时不要显示在dashboard中

    将对应项目的上图配置,取消勾选即可.

  5. 复习HTML+CSS(6)

    n  表格和表单的嵌套顺序 n  单行文本域 语法格式:<input type="text" 属性="值"> 常用属性 l  Name:文本框的名字 ...

  6. 3分钟搞掂Set集合

    前言 声明,本文用的是jdk1.8 前面章节回顾: Collection总览 List集合就这么简单[源码剖析] Map集合.散列表.红黑树介绍 HashMap就是这么简单[源码剖析] LinkedH ...

  7. 谁说深入浅出虚拟机难?现在我让他通俗易懂(JVM)

    1:什么是JVM大家可以想想,JVM 是什么?JVM是用来干什么的?在这里我列出了三个概念,第一个是JVM,第二个是JDK,第三个是JRE.相信大家对这三个不会很陌生,相信你们都用过,但是,你们对这三 ...

  8. java====color、font、button、label、textfield、textarea使用

    1. Color类 µ 公共静态属性:共13个静态属性,分别代表13种不同的颜色常量. µ 构造函数 ü public Color(int r,int g,int b):以整数形式给出红.绿.蓝三个分 ...

  9. Java 嵌套类基础详解

    目录 1. 什么是嵌套类? 2. 为什么要使用嵌套类? 3. 嵌套类的类型 4. 静态嵌套类 5. 非静态嵌套类 5.1 成员内部类 5.2 局部内部类 5.3 匿名内部类 6. 嵌套接口 1. 什么 ...

  10. [LeetCode] Minimum Window Subsequence 最小窗口序列

    Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequence of ...