Python闭包与装饰器原理及实例解析
作者:杰鑫哥 发布时间:2023-04-08 19:44:30
标签:Python,闭包,装饰器
一、闭包
闭包相当于函数中,嵌套另一个函数,并返回。代码如下:
def func(name): # 定义外层函数
def inner_func(age): # 内层函数
print('name: ', name, ', age: ', age)
return inner_func # 注意此处要返回,才能体现闭包
bb = func('jayson') # 将字符串传给func函数,返回inner_func并赋值给变量
bb(28) # 通过变量调用func函数,传入参数,从而完成闭包
>>
name: jayson , age: 28
二、装饰器
装饰器:把函数test当成变量传入装饰函数deco --> 执行了装饰操作后,变量传回给了函数test()。比如装饰器效果是test = test-1,test函数经过deco装饰后,调用test其实执行的是 test = test-1。
1、装饰器是利用闭包原理,区别是装饰器在闭包中传入的参数是函数,而不是变量。
注:其实在装饰器中,函数即变量
def deco(func): # 传入func函数。
print('decoration')
return func
def test():
print('test_func')
test = deco(test) # 对函数进行装饰。执行了deco函数,并将返回值赋值给test
>>
# 输出deco的运行结果
decoration
test() # 运行装饰后的函数
>>
test_func
2、以上代码等价于
def deco(func): # 传入func函数。
print('decoration')
return func
@deco # 等价于上一代码中test = deco(test),不过上一代码需放在定义test之后
def test():
print('test_func')
>>
# 输出deco的运行结果
decoration
test() # 运行装饰后的函数
>>
test_func
3、装饰器(简版)
def deco(func): # 装饰函数传入func
print('decoration')
return func
@deco # 装饰函数。
def test():
print('test_func')
# 定义完函数后,会直接执行装饰器deco(test)
>>
decoration
# 调用test,执行test函数
test()
>>
test_func
3、装饰器(升级版)
在上一个版本中,由于在定义装饰器 + 函数时,就会执行装饰函数里面的语句。
为了使其在未被调用时候不执行,需要再嵌套一个函数,将函数进行包裹。
def deco(func):
print('decoration') # 此处未调用func函数时,会直接执行
def wrapper(): # 名称自定义,一般用wrapper
print('execute') # 此处未调用func函数时,不会执行
func() # 执行函数
return wrapper # 此处返回wrapper给func,通过外部func()执行
@deco # 注意:此处不能有括号。有括号的形式是func未传入最外层deco(),传入deco的子函数中
def test():
print('test_func')
>>
decoration
#调用test
test()
>>
execute
test_func
注意:如果func函数本身有返回值,同样需要在包裹函数中返回
def deco(func):
print('decoration')
def wrapper():
print('execute')
a = func() # 执行函数,并返回值
print('done')
return a # 将func的返回值一并返回
return wrapper
@deco
def test():
print('test_func')
return 5 # 增加返回值
>>
decoration
#调用test
test()
>>
execute
test_func
done
# 此处是test函数的返回值
3、装饰器(进阶版)
在包裹函数中,参数形式设置为*arg、**kwarg,会使得函数更加灵活。
当修改test函数参数形式时,不用在装饰器中同时修改。
import time
def deco(func):
def inner(*arg, **kwarg): # 此处传入参数
begin_time = time.time()
time.sleep(2)
a = func(*arg, **kwarg) # 调用函数,使用传入的参数
end_time = time.time()
print('运行时间:', end_time - begin_time)
return a
return inner
@deco
def test(a):
print('test function:', a)
return a
# 调用函数
test(5)
>>
test function: 5
运行时间: 2.0003252029418945
# 5是函数返回的值
4、高阶版
有时候我们会发现有的装饰器带括号,其原因是将上述的装饰器外面又套了一个函数
import time
def outer(): # 在原装饰器外套一层函数,将装饰器封装在函数里面。(outer自定义)
def deco(func): # 原装饰器,后面的代码一样
def inner(*arg, **kwarg):
begin_time = time.time()
time.sleep(2)
a = func(*arg, **kwarg)
end_time = time.time()
print('运行时间:', end_time - begin_time)
return a
return inner
return deco # 注意:此处需返回装饰函数
@outer() # 此处就需要加括号,其实是调用了outer()函数,将test传进其子函数
def test(a):
print('test function:', a)
return a
test(4)
>>
test function: 4
运行时间: 2.000566005706787
# 返回4
5、高阶终结版
带参数的装饰器(装饰器加括号,带参数)
import time
def outer(choose): # 在最外层函数中加入参数
if choose==1: # 通过choose参数,选择装饰器
def deco(func):
def inner(*arg, **kwarg):
print('decoration1')
begin_time = time.time()
time.sleep(2) # 睡眠2s
a = func(*arg, **kwarg)
end_time = time.time()
print('运行时间1:', end_time - begin_time)
return a
return inner
return deco
else:
def deco(func):
def inner(*arg, **kwarg):
print('decoration2')
begin_time = time.time()
time.sleep(5) # 睡眠5s
a = func(*arg, **kwarg)
end_time = time.time()
print('运行时间2:', end_time - begin_time)
return a
return inner
return deco
@outer(1) # 由于outer中有参数,此处必须传入参数
def test1(a):
print('test function1:', a)
return a
@outer(5) # 传入另一个参数
def test2(a):
print('test function2:', a)
return a
# 分别调用2个函数(2个函数装饰器相同,装饰器参数不同)
test1(2) # 调用test1
>>
decoration1
test function1: 2
运行时间1: 2.000072717666626 # 2秒
# test1的返回值
test2(4) # 调用test2
>>
decoration2
test function2: 4
运行时间2: 5.000797986984253 # 5秒
# test2的返回值
来源:https://www.cnblogs.com/jaysonteng/p/12688627.html
0
投稿
猜你喜欢
- microtime() 函数返回当前 Unix 时间戳的微秒数。用于检测程序执行时间的函数,也是PHP内置的时间函数之一,在PHP中可以用于
- Real Numbers实数实数是具有小数部分的数字, 当然, 实数不是专门用来表示小数的, 也可以用DECIMAL来存储那些无法用INTE
- 在linux服务器lvs负载均衡、双机热备应用中经常用到mysql双机热备,安装和配置过程如下:一、 安装MYSQL# cp mysql-s
- Python 安装Graphviz 详细教程Python安装Graphviz画图器首先,要明确他是一个独立的软件,如果大家用pip的方法装了
- 一、查询操作可以使用Dataframe的index属性和columns属性获取行、列索引。import pandas as pddata =
- 为了防止某些别有用心的人从外部访问数据库,盗取数据库中的用户姓名、密码、信用卡号等其他重要信息,在我们创建数据库驱动的解决方案时,我们首先需
- PyType_Type和PyBaseObject_TypePyObject和PyTypeObject内容的最后指出下图中对实例对象和类型对象
- 本文实例为大家分享了python实现简单计算器的具体代码,供大家参考,具体内容如下今天学习到python中界面设计部分,常用的几种图形化界面
- 0、背景今天看到了一个比较诡异的写法,for后直接跟了else语句,起初还以为是没有缩进好,查询后发现果然有这种语法,特此分享。之前写过c+
- 最近被“模块化”缠身,又是文章又是PPT的,被逼着想了很多相关的东西。整理下我这段时间对于“模块化”的思考,大多都是我自己从事页面重构这份工
- 先让我们看一个例子,了解什么是模式化窗口。以下是QQ秀商城在非登录时提示登录的一种状态。当我在非登录状态,通过保存形象的方式买一件衣服时,弹
- 最近服务器时不时出现Nginx 502 Bad Gateway,如果在电脑旁边还好,要是半夜或者出去了,怎么办?没关系,写个脚本检测服务状态
- 一、安装pycryto能实现大致3种类型的数据加密(单向加密、对称加密 和非对称加密),产生随机数,生成密钥对,数字签名单向加密:Crypt
- 本文实例讲述了python和bash统计CPU利用率的方法。分享给大家供大家参考。具体如下:开始的时候写了一个 bash 的实现;因为最近也
- 如何做一个随机密码产生器?<%genPassword = ""RandomizeFor&nbs
- 要使用request对象的ServerVariables属性,通过它来获得环境变量的值。使用的语法为:Request.ServerVaria
- 在学习python的时候,被推荐了使用PyCharm这款IDE,但是在import包的时候却发生了问题- -无法引入,但是明明通过了pip进
- 概述可以获取的数据包括:video-视频模块user-用户模块dynamic-动态模块这次用“Running Man”十周年特辑的视频,来做
- 本文实例讲述了JavaScript+canvas实现七色板效果。分享给大家供大家参考,具体如下:效果图如下:html:<canvas
- 内容摘要:严格地说,ASP 并不是一门编程语言,所以不存在类这一概念,我们这里说 ASP 类是指 A