首页 技术 正文
技术 2022年11月12日
0 收藏 317 点赞 4,372 浏览 2869 个字

一、简介

装饰器是是修改其它函数功能的函数;其意义是让其他函数在不修改任何代码的前提下增加额外功能

二、数据类型

首先我们来看一段简单的代码:

from types import MethodType,FunctionTypeclass A(object):
def f1(self):
passdef f2(a, b):
return a + bif __name__ == '__main__':
a = A()
print(type(a.f1)) #<class 'method'>
print(type(f2)) #<class 'function'>

结论:不难看出,f1的类型是方法,f2的类型是函数;那有人会问了解这个有啥作用呢?其实了解这个有助于我们下面了解装饰器的原理

三、认识装饰器:

let’s go… 我们来看一个案例:

def B():
print("now you are inside the B() function") def sing():
return "now you are in the sing() function" def eat():
return "now you are in the eat() function" def sleep():
return "now you are in the sleep() function" print(sing())
print(eat())
print(sleep())
#输出的结果为:
now you are inside the hi() function
now you are in the sing() function
now you are in the eat() function
now you are in the sleep() function

结论:那现在我们知道了可以在函数中定义另外的函数。也就是说:我们可以创建嵌套的函数

我们再接着看一段代码,如何让函数作为参数传给另外一个函数的:

def A():
return "hi 小陈!"def doSomethingBeforeA(func):
print("I am doing some boring work before executing A()")
print(func())#执行:
doSomethingBeforeA(A)
#输出结果:
I am doing some boring work before executing A()
hi 小陈!

什么?你还没看懂,那我们再看一个案例:

from types import FunctionTypedef text():
return "Hello world!"def add_itali(func: FunctionType):
def new_func():
#print("now you are in the new_func() function")
return text() #返回text()函数
return new_func

#执行:
print(type(add_itali(text)))
print(add_itali(text)())
#输出结果:
<class 'function'>
Hello world!

难么现在你看懂了么?如果还是没懂,没关系我们再来看看下一段代码:

def new_decorator(func):
def wrapTheFunction():
print("I am doing some boring work before executing func()")
func()
print("I am doing some boring work after executing func()")
return wrapTheFunctiondef requiring_decoration():
print("I am the function which needs decoration")

#执行:
requiring_decoration()#输出结果:
I am the function which needs decoration
#执行:
new_decorator(requiring_decoration)()#输出结果:
I am doing some boring work before executing func()
I am the function which needs decoration
I am doing some boring work after executing func()

有人会有疑问,new_decorator(requiring_decoration)()为哈后面要加“()”呢去,请继续往下看:

#执行:
print(new_decorator(requiring_decoration))#输出结果:
function new_decorator.<locals>.wrapTheFunction at 0x000001FC976BB620>

为什么会这样呢,请随我娓娓道来。。。

四、装饰器的优雅使用:

那么好,我们把上面代码再优化下,用装饰器常用的表达方式表示出来:

def new_decorator(func):
def wrapTheFunction():
print("I am doing some boring work before executing func()")
func() #被装饰的函数
print("I am doing some boring work after executing func()")
return wrapTheFunction@new_decorator
def requiring_decoration():
print("I am the function which needs decoration")
#执行:
requiring_decoration()#输出结果:
I am doing some boring work before executing func()
I am the function which needs decoration
I am doing some boring work after executing func()

老铁们,现在你们懂了么,没懂我们再来看一段代码:

#需求:  <b><i> Hello World!</i></b>
from types import FunctionType

def add_bold(func: FunctionType):
def new_func():
return f"<b>{func()}</b>"
return new_funcdef add_itali(func: FunctionType):
def new_func():
return f"<i>{func()}</i>"
return new_func@add_itali # 语法
@add_bold
def text():
return "Hello world!"
#执行:
print(text())#输出结果:
<i><b>Hello world!</b></i>

五、总结:

#语法:装饰器就是一个函数
def 装饰器名(func):
def wrapper(*args, **kwargs):
// 要做的装饰 ,,省略若干代码
result = func(*args,**kwargs)
return result
return wrapper
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,993
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,507
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,350
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,135
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,768
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,845