我终于弄懂了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装饰器的代码 ...
随机推荐
- 温故知新-java的I/O模型-BIO&NIO&AIO
文章目录 摘要 传统的BIO编程 伪异步I/O编程 NIO编程 AIO编程 几种IO模型的对比 netty 参考 你的鼓励也是我创作的动力 Posted by 微博@Yangsc_o 原创文章,版权声 ...
- zabbix 监控 tcp 连接数
一.zabbix-agent 服务器配置 1.编辑zabbix_agent配置文件,添加以下内容 vim /etc/zabbix/zabbix_agentd.conf ##添加此行 UserParam ...
- C# ASP.NET递归循环生成嵌套json结构树
1. 建立用来保存树结构数据的目标对象 public class TreeObject { public string name { get; set; } public string value { ...
- 开发者大赛 | aelf轻型DApp开发训练大赛结果公布!
6月9日,由aelf基金会发起的轻型DApp开发训练大赛圆满收官.本次训练赛基于aelf公开测试网展开,主要针对轻型DApp,旨在激励更多的开发者参与到aelf生态中来. 活动于4月21日上线后,ae ...
- markdown分页导出pdf
在需要分页之处,插入代码: <div STYLE="page-break-after: always;"></div>
- activeMQ从入门到简单集群指南
1.什么是amq MQ是消息中间件,基于JAVA的JMS消息服务机制来传递信息. 2.mq的作用 MQ给程序之间提供了一个缓冲,避免了在程序交互频繁的情况下,提高程序性能瓶颈和数据的可靠性 3.mq怎 ...
- Selenium和ChromeDriver的安装与配置
安装安装selenium: win: pip install seleniumliunx: pip3 install selenium12安装ChromeDriver, 该工具供selenium使用C ...
- DML_The OUTPUT Clause
DML_The OUTPUT Clause /**/ ------------------------------------------------------------------------- ...
- 《Java并发编程的艺术》第10章 Executor框架
Java的线程既是工作单元,也是执行机制.从JDK5开始,把工作单元与执行机制分离开来.工作单元包括Runnable和Callable,执行机制由Executor框架提供. 10.1 Executor ...
- 基于web网站项目的性能测试结果分析
业务背景: 最近公司研发了一款对并发要求比较高的web项目,需要对其压力测试,模拟线上可能存在的问题,这个过程中遇到一些很多问题,这里重新梳理一下思路,希望能给遇到同样问题的小伙伴提供一个参考. 工具 ...