我终于弄懂了Python的装饰器(一)
此系列文档:
一、装饰器基础(什么是装饰器)
Python的函数是对象
要了解装饰器,您必须首先了解函数是Python中的对象。这具有重要的联系。
让我们来看一个简单的例子:
def shout(word="yes"):
return word.capitalize()+"!"
print(shout())
# 输出 : 'Yes!'
# 作为一个对象,您可以像其他对象一样将函数赋给变量
scream = shout
#注意我们不使用括号:我们没有调用函数
#我们将函数“shout”放入变量“scream”。
#这意味着您可以从“scream”中调用“shout”:
print(scream())
# 输出: 'Yes!'
#除此之外,这意味着您可以删除旧名称'shout',该功能仍可从'scream'访问
del shout
try:
print(shout())
except NameError as e:
print(e)
#输出: "name 'shout' is not defined"
print(scream())
# 输出: 'Yes!'
请记住这一点,我们将在不久后回头再说。
Python函数的另一个有趣特性是可以在另一个函数中定义它们!
def talk():
# 您可以在“talk”中动态定义一个函数...
def whisper(word="yes"):
return word.lower()+"..."
# ...并且可以立马使用它。
print(whisper())
#您每次调用“talk”,都会定义“whisper”,然后在“talk”中调用“whisper”。
talk()
# 输出:
# "yes..."
# 但是"whisper"不存在"talk"定义以外的地方:
try:
print(whisper())
except NameError as e:
print(e)
#输出 : "name 'whisper' is not defined"
函数参考
OK,应该还在看吧?现在开始有趣的部分...
您已经看到函数是对象。
因此,函数:
- 可以分配给变量
- 可以在另一个函数中定义
这意味着一个函数可以return另一个功能。
def getTalk(kind="shout"):
# 我们顶一个即时的函数
def shout(word="yes"):
return word.capitalize()+"!"
def whisper(word="yes") :
return word.lower()+"...";
# 然后我们返回它
if kind == "shout":
#我们不使用“()”,所以我们没有调用函数,我们正在返回这个函数对象
return shout
else:
return whisper
#获取函数并将其分配给变量: "talk"
talk = getTalk()
#您可以看到“talk”是一个函数对象:
print(talk)
#输出 : <function shout at 0xb7ea817c>
#函数对象返回的内容:
print(talk())
#输出 : Yes!
#如果您感到困惑,甚至可以直接使用它:
print(getTalk("whisper")())
#outputs : yes...
还有更多的内容!
如果可以return一个函数,则可以将其中一个作为参数传递:
def doSomethingBefore(func):
print("I do something before then I call the function you gave me")
print(func())
doSomethingBefore(scream)
#输出:
#I do something before then I call the function you gave me
#Yes!
好吧,您只具备了解装饰器所需的所有信息。
您会看到,装饰器是“包装器(wrappers)”,这意味着它们使您可以在装饰函数之前和之后执行代码,而无需修改函数本身的代码内容。
手工进行装饰
您将知道如何进行手动操作:
#装饰器是讲另外一个函数作为参数的函数
def my_shiny_new_decorator(a_function_to_decorate):
# 在内部,装饰器动态定义一个函数:包装器(wrappers)。
# 此功能将被包装在原始功能的外部,以便它可以在代码之前和之后执行代码。
def the_wrapper_around_the_original_function():
# 在调用原始函数之前,将要执行的代码放在此处
print("Before the function runs")
#在此处调用函数(使用括号)
a_function_to_decorate()
# 在调用原始函数后,将要执行的代码放在此处
print("After the function runs")
#至此,“a_function_to_decorate”从未执行过。
#我们返回刚刚创建的包装函数。
#包装器包含函数和在代码之前和之后执行的代码。随时可以使用!
return the_wrapper_around_the_original_function
#现在,假设您创建了函数,但是不想再修改的函数。
def a_stand_alone_function():
print("I am a stand alone function, don't you dare modify me")
a_stand_alone_function()
#输出: I am a stand alone function, don't you dare modify me
#所以,您可以装饰它以扩展其行为。
#只需将其传递给装饰器,它将动态地包装在
#您想要的任何代码中,并为您返回准备使用的新功能:
a_stand_alone_function_decorated = my_shiny_new_decorator(a_stand_alone_function)
a_stand_alone_function_decorated()
#输出:
#Before the function runs
#I am a stand alone function, don't you dare modify me
#After the function runs
现在,您可能希望每次调用a_stand_alone_function时a_stand_alone_function_decorated都调用它。
这很简单,只需a_stand_alone_function用以下方法返回的函数覆盖my_shiny_new_decorator:
a_stand_alone_function = my_shiny_new_decorator(a_stand_alone_function)
a_stand_alone_function()
#输出:
#Before the function runs
#I am a stand alone function, don't you dare modify me
#After the function runs
#这正是装饰器的工作!
装饰器神秘化
这里展示一下使用装饰器的语法:
@my_shiny_new_decorator
def another_stand_alone_function():
print("Leave me alone")
another_stand_alone_function()
#输出:
#Before the function runs
#Leave me alone
#After the function runs
是的,仅此而已。@decorator只是实现以下目的的捷径:
another_stand_alone_function = my_shiny_new_decorator(another_stand_alone_function)
装饰器只是装饰器设计模式的pythonic变体。
Python中嵌入了几种经典的设计模式来简化开发(例如迭代器)。
当然,您可以累加装饰器:
def bread(func):
def wrapper():
print("</''''''\>")
func()
print("<\______/>")
return wrapper
def ingredients(func):
def wrapper():
print("#tomatoes#")
func()
print("~salad~")
return wrapper
def sandwich(food="--ham--"):
print(food)
sandwich()
#输出: --ham--
sandwich = bread(ingredients(sandwich))
sandwich()
#输出:
#</''''''\>
# #tomatoes#
# --ham--
# ~salad~
#<\______/>
使用Python装饰器语法:
@bread
@ingredients
def sandwich(food="--ham--"):
print(food)
sandwich()
#outputs:
#</''''''\>
# #tomatoes#
# --ham--
# ~salad~
#<\______/>
您设置装饰器事项的顺序是很重要的,如::
@ingredients
@bread
def strange_sandwich(food="--ham--"):
print(food)
strange_sandwich()
#outputs:
##tomatoes#
#</''''''\>
# --ham--
#<\______/>
# ~salad~
本文首发于BigYoung小站
我终于弄懂了Python的装饰器(一)的更多相关文章
- 我终于弄懂了Python的装饰器(二)
此系列文档: 1. 我终于弄懂了Python的装饰器(一) 2. 我终于弄懂了Python的装饰器(二) 3. 我终于弄懂了Python的装饰器(三) 4. 我终于弄懂了Python的装饰器(四) 二 ...
- 我终于弄懂了Python的装饰器(四)
此系列文档: 1. 我终于弄懂了Python的装饰器(一) 2. 我终于弄懂了Python的装饰器(二) 3. 我终于弄懂了Python的装饰器(三) 4. 我终于弄懂了Python的装饰器(四) 四 ...
- 理解Python中的装饰器//这篇文章将python的装饰器来龙去脉说的很清楚,故转过来存档
转自:http://www.cnblogs.com/rollenholt/archive/2012/05/02/2479833.html 这篇文章将python的装饰器来龙去脉说的很清楚,故转过来存档 ...
- 进阶Python:装饰器 全面详解
进阶Python:装饰器 前言 前段时间我发了一篇讲解Python调试工具PySnooper的文章,在那篇文章开始一部分我简单的介绍了一下装饰器,文章发出之后有几位同学说"终于了解装饰器的用 ...
- Python各式装饰器
Python装饰器,分两部分,一是装饰器本身的定义,一是被装饰器对象的定义. 一.函数式装饰器:装饰器本身是一个函数. 1.装饰函数:被装饰对象是一个函数 [1]装饰器无参数: a.被装饰对象无参数: ...
- Python札记 -- 装饰器补充
本随笔是对Python札记 -- 装饰器的一些补充. 使用装饰器的时候,被装饰函数的一些属性会丢失,比如如下代码: #!/usr/bin/env python def deco(func): def ...
- python基础——装饰器
python基础——装饰器 由于函数也是一个对象,而且函数对象可以被赋值给变量,所以,通过变量也能调用该函数. >>> def now(): ... print('2015-3-25 ...
- 【转】详解Python的装饰器
原文链接:http://python.jobbole.com/86717/ Python中的装饰器是你进入Python大门的一道坎,不管你跨不跨过去它都在那里. 为什么需要装饰器 我们假设你的程序实现 ...
- 两个实用的Python的装饰器
两个实用的Python的装饰器 超时函数 这个函数的作用在于可以给任意可能会hang住的函数添加超时功能,这个功能在编写外部API调用 .网络爬虫.数据库查询的时候特别有用 timeout装饰器的代码 ...
随机推荐
- Python子类构造函数调用super().__init__()用法说明
一.super的作用 1.如果子类(Puple)继承父类(Person)不做初始化,那么会自动继承父类(Person)属性name.2.如果子类(Puple_Init)继承父类(Person)做了初始 ...
- QToolTip 设置提示信息
import sys from PyQt5.QtWidgets import (QWidget, QToolTip, QPushButton, QApplication) from PyQt5.QtG ...
- jreble备注
版本:2019.1.4 激活URL:http://jrebel.pyjuan.com/36931214-7bb6-42d4-afd7-26eb5628e004
- CentOS7——初始化
CentOS7--初始化 #禁止关闭显示器 archlinux wiki 提及的方法 echo -ne "\033[9;0]" >> /etc/issue # 重启,c ...
- Email地址验证
<script>function validateForm(){ var x=document.forms["myForm"]["email"].v ...
- 第m大的身份证号码(局部排序代全局、结构体排序)
第m大的身份证号码(点击) 时间限制: 1 Sec 内存限制: 128 MB ...
- Swift Core Data 图片存储与读取
1.首先推出选择拍照还是相册的alert,代码如下: UIAlertController *alert = [UIAlertController alertControllerWithTitle:ni ...
- Spring Boot 在启动时进行配置文件加解密
Spring Boot Application 事件和监听器 寻找到application.yml的读取的操作. 从spring.factories 中查看到 # Application Listen ...
- 附024.Kubernetes全系列大总结
Kubernetes全系列总结如下,后期不定期更新.欢迎基于学习.交流目的的转载和分享,禁止任何商业盗用,同时希望能带上原文出处,尊重ITer的成果,也是尊重知识.若发现任何错误或纰漏,留言反馈或右侧 ...
- 【JMeter_02】JMeter目录结构与功能模块介绍
安装包目录结构 1.backups: 存放jmeter对脚本的自动备份保存文件 2.bin:jmeter的可执行文件目录,包含启动.配置等相关文件 3.dosc:程序自带的官方文档本地存放目录 4.e ...