python之装饰器

作者: howie6879 | 来源:发表于2016-07-31 19:51 被阅读604次

1.认识装饰器

在python中,对于一个函数,若想在其运行前后做点什么,那么装饰器是再好不过的选择,话不多说,上代码。

#!/usr/bin/env
# -*-coding:utf-8-*-
# script: 01.py
__author__ = 'howie'
from functools import wraps
def decorator(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        print("%s was called" % func.__name__)
        func(*args, **kwargs)
    return wrapper
@decorator
def hello(name="howie"):
    print("Hello %s!" % name)
hello()
outputs:
hello was called
Hello howie!

这段代码,初看之下,确实不是很理解,接下来一步一步分析,看看装饰器到底是怎么工作的。

2.装饰器原理

在python中,方法允许作为参数传递,想在某个函数执行前后加点料,也可以这样简单实现。

#!/usr/bin/env
# -*-coding:utf-8-*-
# script: 02-1.py
__author__ = 'howie'
def decorator(func):
    print("%s was called" % func.__name__)
    func()
def hello(name="howie"):
    print("Hello %s!" % name)
decorator(hello)

由此,上面代码也可以这样写:

#!/usr/bin/env
# -*-coding:utf-8-*-
# script: 02-2.py
__author__ = 'howie'
def decorator(func):
    print("%s was called" % func.__name__)
    func()
@decorator
def hello(name="howie"):
    print("Hello %s!" % name)
hello

两段代码执行后:

outputs:
hello was called
Hello howie!

表面上看来,02-2.py代码看起来也可以很好地执行啊,可请注意,在末尾处,hello只是函数名称,它并不能被调用,若执行hello(),就会报TypeError: 'NoneType' object is not callable对象不能调用错误,这是自然,在decoratorfunc()直接将传入的函数实例化了,有人会想,那如果这样改呢?

#!/usr/bin/env
# -*-coding:utf-8-*-
# script: 02-3.py
__author__ = 'howie'
def decorator(func):
    print("%s was called" % func.__name__)
    return func
@decorator
def hello(name="howie"):
    print("Hello %s!" % name)
hello()

确实,这样改是可以,可有没有想过,若想在函数执行结束后加点装饰呢?这样便行不通了,可能又有人会想,若这样改呢?

#!/usr/bin/env
# -*-coding:utf-8-*-
# script: 02-4.py
__author__ = 'howie'
def decorator(func):
    print("%s was called" % func.__name__)
    func()
    return bye
def bye():
    print("bye~")
@decorator
def hello(name="howie"):
    print("Hello %s!" % name)
hello()

这样写看起来,恩,怎么说呢,总有种没有意义的感觉,不如直接将在外部的函数放进decorator中,如下:

#!/usr/bin/env
# -*-coding:utf-8-*-
# script: 02-5.py
__author__ = 'howie'
def decorator(func):
    def wrapper():
      print("%s was called" % func.__name__)
      func()
      print("bye~")
    return wrapper
@decorator
def hello(name="howie"):
    print("Hello %s!" % name)
hello()

执行:

outputs:
hello was called
Hello howie!
bye~

怎么样,输出的结果是不是符合要求,其实简单来看的话,可以这样理解hello()==decorator(hello)()==wrapper(),最后其实就是执行wrapper()函数而已,事实就是如此的简单,不妨来验证一下:

#!/usr/bin/env
# -*-coding:utf-8-*-
# script: 02-6.py
__author__ = 'howie'
def decorator(func):
    def wrapper():
      print("%s was called" % func.__name__)
      func()
      print("bye~")
    return wrapper
@decorator
def hello(name="howie"):
    print("Hello %s!" % name)
hello()
print(hello.__name__)
outputs:
hello was called
Hello howie!
bye~
wrapper

果然就是执行了wrapper函数,解决问题的同时也会出现新的问题,那便是代码中本来定义的hello函数岂不是被wrapper函数覆盖了,又该如何解决这个问题呢?这时候functions.wraps就可以登场了,代码如下:

#!/usr/bin/env
# -*-coding:utf-8-*-
# script: 02-7.py
__author__ = 'howie'
from functools import wraps
def decorator(func):
    @wraps(func)
    def wrapper():
      print("%s was called" % func.__name__)
      func()
      print("bye~")
    return wrapper
@decorator
def hello(name="howie"):
    print("Hello %s!" % name)
hello()
print(hello.__name__)

执行代码:

outputs:
hello was called
Hello howie!
bye~
hello

functions.wraps作用是不是一目了然哈到了这一步,再看01.py的代码,是不是代码结构清晰明了,只不过多了个参数

#!/usr/bin/env
# -*-coding:utf-8-*-
# script: 01.py
__author__ = 'howie'
from functools import wraps
def decorator(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        print("%s was called" % func.__name__)
        func(*args, **kwargs)
    return wrapper
@decorator
def hello(name="howie"):
    print("Hello %s!" % name)
hello('world')

猜都猜得到执行后输出什么了。

3.结语

只要了解装饰器原理,不管是带参数的装饰器,还是装饰器类,都是小菜一碟。
若有错误,尽请指出。

相关文章

  • 装饰器五部曲

    听说你学不会装饰器?Python基础之装饰器五部曲,带你轻轻松松学会装饰器 装饰器(decorator)是Pyth...

  • 9个Python 内置装饰器: 显著优化代码

    装饰器是应用“Python 之禅”哲学的最佳 Python 特性。装饰器可以帮助您编写更少、更简单的代码来实现复杂...

  • Python 入门之 Python三大器 之 装饰器

    Python 入门之 Python三大器 之 装饰器 1、开放封闭原则: (1)代码扩展进行开放 ​ 任何一个程序...

  • 装饰器模式

    介绍 在python装饰器学习 这篇文章中,介绍了python 中的装饰器,python内置了对装饰器的支持。面向...

  • Python ☞ day 5

    Python学习笔记之 装饰器& 偏函数 & 异常处理 & 断言 & 文件读写 &编码与解码 装饰器 概念:是一个...

  • Python闭包和装饰器

    本节课纲: 魔法方法之_call_ 闭包 装饰器 装饰器实例 一、魔法方法之_call_ 在Python中,函数其...

  • python中的装饰器

    python装饰器详解 Python装饰器学习(九步入门) 装饰器(decorator) 就是一个包装机(wrap...

  • [译] Python装饰器Part II:装饰器参数

    这是Python装饰器讲解的第二部分,上一篇:Python装饰器Part I:装饰器简介 回顾:不带参数的装饰器 ...

  • Python中的装饰器

    Python中的装饰器 不带参数的装饰器 带参数的装饰器 类装饰器 functools.wraps 使用装饰器极大...

  • Python进阶——面向对象

    1. Python中的@property   @property是python自带的装饰器,装饰器(decorat...

网友评论

本文标题:python之装饰器

本文链接:https://www.haomeiwen.com/subject/uexxsttx.html