美文网首页
Python 装饰器

Python 装饰器

作者: Sugars_daddy | 来源:发表于2019-02-25 11:48 被阅读0次

目录

函数
简单的装饰器

函数

理解装饰器前,你首先需要理解函数是如何工作的;

# 理解函数
>>> def add_one(number):
...     return number + 1

>>> add_one(2)
3

函数是一级对象(first-class objects),也就是说函数是可以作为参数来传递使用

# 函数是一级对象
def say_hello(name):
    return f"Hello {name}"

def be_awesome(name):
    return f"Yo {name}, together we are the awesomest!"

def greet_bob(greeter_func):
    return greeter_func("Bob")

# say_hello 这个函数作为参数被使用
>>> greet_bob(say_hello)    # greet_bob() 与 say_hello 的区别是一个带括号,一个没有括号;其中say_hello函数只有传递的作用,而greet_bob()函数是调用执行。
'Hello Bob'
# be_awesome这个函数作为参数被使用
>>> greet_bob(be_awesome)
'Yo Bob, together we are the awesomest!'

内嵌函数(inner function)就是在其他函数中定义函数,例如:

def parent():
    print("Printing from the parent() function")

    def first_child():
        print("Printing from the first_child() function")

    def second_child():
        print("Printing from the second_child() function")

    second_child()
    first_child()
# 调用parent()函数
>>> parent()
Printing from the parent() function
Printing from the second_child() function
Printing from the first_child() function

从函数中返回函数

用函数作为返回值,例如:

def parent(num):
    def first_child():
        return "Hi, I am Emma"

    def second_child():
        return "Call me Liam"

    if num == 1:
        return first_child
    else:
        return second_child

注意返回的 first_child 不带括号,就是直接返回引用的函数 first_child;相反first_child()是对函数求值的结果。

# 接上
>>> first = parent(1)
>>> second = parent(2)

>>> first
<function parent.<locals>.first_child at 0x7f599f1e2e18>

>>> second
<function parent.<locals>.second_child at 0x7f599dad5268>

>>> first()
'Hi, I am Emma'

>>> second()
'Call me Liam'

简单的装饰器

装饰器,其本身接收一个函数对象作为参数,然后做一些工作后,返回接收的参数,供外界调用。
简而言之:装饰器包装一个函数,改变其行为。

# 装饰器

def my_decorator(func):
    def wrapper():
        print("Something is happening before the function is called.")
        func()
        print("Something is happening after the function is called.")
    return wrapper

def say_whee():
    print("Whee!")

say_whee = my_decorator(say_whee)


# 调用say_whee()
>>> say_whee()
Something is happening before the function is called.
Whee!
Something is happening after the function is called.

为了不打扰邻居休息,下面的例子只在白天运行经过修饰的代码。
如果你试着在夜间调用say_whee(),什么也不会发生。

from datetime import datetime

def not_during_the_night(func):
    def wrapper():
        if 7 <= datetime.now().hour < 22:
            func()
        else:
            pass  # Hush, the neighbors are asleep
    return wrapper

def say_whee():
    print("Whee!")

say_whee = not_during_the_night(say_whee)

语法糖
用say_whee()的方式来修饰显得有点笨拙,而且装饰在定义函数下面隐藏了一些。

def my_decorator(func):
    def wrapper():
        print("Something is happening before the function is called.")
        func()
        print("Something is happening after the function is called.")
    return wrapper

@my_decorator
def say_whee():
    print("Whee!")

@my_decorator 是一种更简单的写法 代替say_whee = my_decorator(say_whee)

相关文章

  • 装饰器模式

    介绍 在python装饰器学习 这篇文章中,介绍了python 中的装饰器,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 装饰器填坑指南 | 最常见的报错信息、原因和解决方

    Python 装饰器简介装饰器(Decorator)是 Python 非常实用的一个语法糖功能。装饰器本质是一种返...

  • Python装饰器

    Python装饰器 一、函数装饰器 1.无参装饰器 示例:日志记录装饰器 2.带参装饰器 示例: 二、类装饰器 示例:

  • python3基础---详解装饰器

    1、装饰器原理 2、装饰器语法 3、装饰器执行的时间 装饰器在Python解释器执行的时候,就会进行自动装饰,并不...

  • 2019-05-26python装饰器到底是什么?

    装饰器例子 参考语法 装饰器是什么?个人理解,装饰器,是python中一种写法的定义。他仍然符合python的基本...

  • 2018-07-18

    Python装饰器 装饰,顾名思义,是用来打扮什么东西的。Python装饰...

网友评论

      本文标题:Python 装饰器

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