搜索
首页后端开发Python教程装饰器decorator详解及实例

装饰器

一、定义

1.装饰器:本质是函数

2.功能:用来装饰其他函数,为其他函数添加附加功能

二、原则

1.不能修改被装饰函数的源代码

2.不能修改被装饰函数的调用方式

三、实现装饰器

1.函数 即 变量 的概念

2.高阶函数

3.嵌套函数

>> 高阶函数 + 嵌套函数 = 装饰器

四、函数 即 变量

1、函数和变量的类比

x = 1
print(id(x))
def test():
	pass
print(test)
#输出
1842348496
<function test at 0x0000020DBA24D2F0>

  在上例中我们定义了一个变量 "x" 和一个函数 test(),我们分别打印出变量和函数在内存中的位置。可以看出,print(test) 即 print("函数名")的时候,我们可以打印出函数的内存地址。

  我们看下面的代码:

def test():
	print("in the test.")
f=test
f()
#输出
in the test.

  我们把函数名 test 赋予给 f,然后运行 f(),可以看出函数是可以正常运行的,而且就是test函数的运行结果。那么这和下面的代码是不是类似:

x = 1
y = x
print(y)
#输出
1

  我们可以做出如下类比,函数名 test 相当于 x ,函数体就相当于 1,而 f 就相当于 y。

2.python中内存的表现形式

    装饰器decorator详解及实例

  我们把绿方块当作是内存,每一个小方块就是变量或者函数在内存当中的地址。而变量名(x)或者函数名(test),我们可以形象的将他们比作门牌号。当需要调用变量或者函数的时候,我们只要引用他们的门牌号就可以找到他们的内存地址并返回。只是函数的运行需要加(),如 test()。

  既然调用变量其实就是引用变量的内存地址,而调用函数名同样可以得到函数体的内存地址。我们就可以把函数名当作变量名传给函数,即函数就是变量。而将函数当作参数的函数,也就是高阶函数。

五、高阶函数

满足下列条件之一就是高阶函数

1.把一个函数名当作实参传给另外一个函数

2.返回值中包含函数名

1)函数名作为参数

import time
def test1():
	time.sleep(2)
	print("in the test1.")
def test2(func):
	start_time = time.time()
	func()
	stop_time = time.time()
	print("The action time of program is {}".format(stop_time-start_time))
test2(test1)
#输出
in the test1.
The action time of program is 2.0012054443359375

  以上事例中,我们定义了一个函数 test1,同时也定义了一个高阶函数test2。我们把test1函数名当作参数传入test2中,可以实现这样一个功能,为原本的test1函数添加了一个计算运行时间的功能。这有点像装饰器了,但是有一点符合,就是上面的高阶函数test2改变了函数的调用方式。

  但是我们实现了在不修改被装饰函数的基础上,添加了新功能。

2)返回值中有函数名

def test1():
	time.sleep(2)
	print("in the test1.")
def test2(func):
	print(func)
	return func
test1 = test2(test1)
test1()
#输出
<function test1 at 0x000001E5D853D2F0>
in the test1.

  在上例中,我们最后将高阶函数test2(test1) 赋予给了test1,再次调用test1函数。我们可以直观的看到test1函数的调用方式在此例中没有改变。但是也并没有添加新功能,而这就需要使用到嵌套函数了。

六、嵌套函数

在函数体内又另一个函数的完整定义,这就是嵌套函数。

1)定义:

def foo():
	print("in the foo")
	def bar():
		print("in the bar")
	bar()
foo()

仅仅在函数内容调用函数,就不是嵌套函数,如下:

def test1():
    print("in the test1.")
def test2():
    test1()

2)嵌套函数的作用域

局部作用域和全局作用域的访问顺序

x = 0
def grandpa():
	x = 1
	def dad():
		x = 2
		def son():
			x = 3
			print(x)
		son()
	dad()
grandpa()
#输出
3

3)使用嵌套函数为被修饰函数添加新功能 

  在高阶函数第二例中,我们实现了不改变原函数的调用方式。而需要添加新功能的话,就要求修饰内容存在在返回值中,即return func 中,我们可以定义一个嵌套函数来实现这个功能。

import time
def timer(func):  # timer(test1) func = test1
	def deco():
		start_time = time.time()
		func()   # run test1()
		stop_time = time.time()
		print("the action time of the program is {}".format(stop_time-start_time))
	return deco   # 返回了deco的内存地址
def test1():
	time.sleep(2)
	print("in the test1.")
test1 = timer(test1)
test1()
# 输出 
in the test1.
the action time of the program is 2.0003786087036133

  我们在timer()内部定义了一个嵌套函数 deco(),这个嵌套函数实现了为被修饰函数添加运行时间的功能。而timer()函数返回了deco()的内存地址,这个内存地址deco就可以被引用,甚至直接赋予给 test1。这样我们就可以直接运行 test1(),这样就实现我们的装饰器的功能。

七、装饰器 

  python通过在函数定义前添加一个装饰器名和@符号,来实现对函数的包装 

import time
def timer(func):  # timer(test1) func = test1
	def deco():
		start_time = time.time()
		func()   # run test1()
		stop_time = time.time()
		print("the action time of the program is {}".format(stop_time-start_time))
	return deco   # 返回了deco的内存地址
@timer   # test1 = timer(test1)
def test1():
	time.sleep(2)
	print("in the test1.")
test1()

以上是装饰器decorator详解及实例的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
Python的混合方法:编译和解释合并Python的混合方法:编译和解释合并May 08, 2025 am 12:16 AM

pythonuseshybridapprace,ComminingCompilationTobyTecoDeAndInterpretation.1)codeiscompiledtoplatform-Indepententbybytecode.2)bytecodeisisterpretedbybythepbybythepythonvirtualmachine,增强效率和通用性。

了解python的' for”和' then”循环之间的差异了解python的' for”和' then”循环之间的差异May 08, 2025 am 12:11 AM

theKeyDifferencesBetnewpython's“ for”和“ for”和“ loopsare:1)” for“ loopsareIdealForiteringSequenceSquencesSorkNowniterations,而2)”,而“ loopsareBetterforConterContinuingUntilacTientInditionIntionismetismetistismetistwithOutpredefinedInedIterations.un

Python串联列表与重复Python串联列表与重复May 08, 2025 am 12:09 AM

在Python中,可以通过多种方法连接列表并管理重复元素:1)使用 运算符或extend()方法可以保留所有重复元素;2)转换为集合再转回列表可以去除所有重复元素,但会丢失原有顺序;3)使用循环或列表推导式结合集合可以去除重复元素并保持原有顺序。

Python列表串联性能:速度比较Python列表串联性能:速度比较May 08, 2025 am 12:09 AM

fasteStmethodMethodMethodConcatenationInpythondependersonListsize:1)forsmalllists,operatorseffited.2)forlargerlists,list.extend.extend()orlistComprechensionfaster,withextendEffaster,withExtendEffers,withextend()withextend()是extextend()asmoremory-ememory-emmoremory-emmoremory-emmodifyinginglistsin-place-place-place。

您如何将元素插入python列表中?您如何将元素插入python列表中?May 08, 2025 am 12:07 AM

toInSerteLementIntoApythonList,useAppend()toaddtotheend,insert()foreSpificPosition,andextend()formultiplelements.1)useappend()foraddingsingleitemstotheend.2)useAddingsingLeitemStotheend.2)useeapecificindex,toadapecificindex,toadaSpecificIndex,toadaSpecificIndex,blyit'ssssssslorist.3 toaddextext.3

Python是否列表动态阵列或引擎盖下的链接列表?Python是否列表动态阵列或引擎盖下的链接列表?May 07, 2025 am 12:16 AM

pythonlistsareimplementedasdynamicarrays,notlinkedlists.1)他们areStoredIncoNtiguulMemoryBlocks,mayrequireRealLealLocationWhenAppendingItems,EmpactingPerformance.2)LinkesedlistSwoldOfferefeRefeRefeRefeRefficeInsertions/DeletionsButslowerIndexeDexedAccess,Lestpypytypypytypypytypy

如何从python列表中删除元素?如何从python列表中删除元素?May 07, 2025 am 12:15 AM

pythonoffersFourmainMethodStoreMoveElement Fromalist:1)删除(值)emovesthefirstoccurrenceofavalue,2)pop(index)emovesanderturnsanelementataSpecifiedIndex,3)delstatementremoveselemsbybybyselementbybyindexorslicebybyindexorslice,and 4)

试图运行脚本时,应该检查是否会遇到'权限拒绝”错误?试图运行脚本时,应该检查是否会遇到'权限拒绝”错误?May 07, 2025 am 12:12 AM

toresolvea“ dermissionded”错误Whenrunningascript,跟随台词:1)CheckAndAdjustTheScript'Spermissions ofchmod xmyscript.shtomakeitexecutable.2)nesureThEseRethEserethescriptistriptocriptibationalocatiforecationAdirectorywherewhereyOuhaveWritePerMissionsyOuhaveWritePermissionsyYouHaveWritePermissions,susteSyAsyOURHomeRecretectory。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具