Python3.5装饰器原理及应用实例详解
作者:loveliuzz 发布时间:2023-11-07 23:15:51
本文实例讲述了Python3.5装饰器原理及应用。分享给大家供大家参考,具体如下:
1、装饰器:
(1)本质:装饰器的本质是函数,其基本语法都是用关键字def去定义的。
(2)功能:装饰其他函数,即:为其他函数添加附加功能。
(3)原则:不能修改被装饰的函数的源代码,不能修改被装饰的函数的调用方式。即:装饰器对待被修饰的函数是完全透明的。
(4)简单应用:统计函数运行时间的装饰器
import time
#统计函数运行时间的砖装饰器
def timmer(func):
def warpper(*args,**kwargs):
strat_time = time.time()
func()
stop_time = time.time()
print("the func run time is %s" %(stop_time-strat_time))
return warpper
@timmer
def test1():
time.sleep(3)
print("in the test1")
test1()
运行结果:
in the test1
the func run time is 3.000171661376953
(5)实现装饰器知识储备:
a、函数即“变量”
b、高阶函数
c、函数嵌套
d、高阶函数+嵌套函数==》装饰器
2、装饰器知识储备——函数即“变量”
定义一个函数,相当于把函数体赋值给这个函数名。
Python解释器如何回收变量:采用引用计数。当引用有没有了时(门牌号不存在),变量就被回收了。
函数的定义也有内存回收机制,与变量回收机制一样。匿名函数没有函数名,就会被回收。
变量的使用:先定义再调用,只要在调用之前已经存在(定义)即可;函数即“变量”,函数的使用是一样的。
函数调用顺序:其他的高级语言类似,Python 不允许在函数未声明之前,对其进行引用或者调用
下面的两段代码运行效果一样:
def bar():
print("in the bar")
def foo():
print("in the foo")
bar()
foo()
#python为解释执行,函数foo在调用前已经声明了bar和foo,所以bar和foo无顺序之分
def foo():
print("in the foo")
bar()
def bar():
print("in the bar")
foo()
运行结果:
in the foo
in the bar
in the foo
in the bar
注意:python为解释执行,函数foo在调用前已经声明了bar和foo,所以bar和foo无顺序之分
原理图为:
3、装饰器知识储备——高阶函数
满足下列其中一种即可称之为高阶函数:
a、把一个函数名当做实参传递给另一个函数(在不修改被装饰函数的情况下为其添加附加功能)
b、返回值中包含函数名(不修改函数的调用方式)
(1)高阶函数示例:
def bar():
print("in the bar")
def test1(func):
print(func) #打印门牌号,即内存地址
func()
test1(bar) #门牌号func=bar
运行结果:
<function bar at 0x00BCDFA8>
in the bar
(2)高阶函数的妙处——把一个函数名当做实参传递给另一个函数(在不修改被装饰函数的情况下为其添加附加功能)
import time
def bar():
time.sleep(3)
print("in the bar")
#test2在不修改被修饰函数bar的代码时添加了附加的及时功能
def test2(func):
start_time = time.time()
func() #run bar
stop_time = time.time()
print("the func run time is %s " %(stop_time-start_time))
#调用方式发生改变,不能像原来的方法去调用被修饰的函数(所以不能实现装饰器的功能)
test2(bar)
#bar()
运行结果:
in the bar
the func run time is 3.000171661376953
(3)高阶函数的妙处——返回值中包含函数名(不修改函数的调用方式)
import time
def bar():
time.sleep(3)
print("in the bar")
def test3(func):
print(func)
return func
bar = test3(bar)
bar() #run bar
运行结果:
<function bar at 0x00BADFA8>
in the bar
4、装饰器知识储备——嵌套函数
#函数嵌套
def foo():
print("in the foo")
def bar(): #bar函数具有局部变量的特性,不能在外部调用,只能在内部调用
print("in the bar")
bar()
foo()
运行结果:
in the foo
in the bar
装饰器应用——模拟网站登录页面,访问需要认证登录页面
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu
#模拟网站,访问页面和部分需要登录的页面
import timer
user,passwd = "liu","liu123"
def auth(func):
def wrapper(*args,**kwargs):
username = input("Username:").strip()
password = input("Password:").strip()
if username == user and password == passwd:
print("\033[32;1mUser has passed authentication!\033[0m")
res = func(*args,**kwargs)
print("-----after authentication---")
return res
else:
exit("\033[31;1mInvalid username or password!\033[0m")
return wrapper
def index():
print("welcome to index page!")
@auth
def home():
print("welcome to index home!")
return "from home"
@auth
def bbs():
print("welcome to index bbs!")
#函数调用
index()
print(home())
bbs()
运行结果:
welcome to index page!
Username:liu
Password:liu123
User has passed authentication!
welcome to home page!
-----after authentication---
from home
Username:liu
Password:liu123
User has passed authentication!
welcome to bbs page!
-----after authentication---
装饰器带参数
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu
#模拟网站,访问页面和部分需要登录的页面,多种认证方式
import timer
user,passwd = "liu","liu123"
def auth(auth_type):
print("auth func:",auth_type)
def outer_wrapper(func):
def wrapper(*args, **kwargs):
print("wrapper func args:",*args, **kwargs)
if auth_type == "local":
username = input("Username:").strip()
password = input("Password:").strip()
if username == user and password == passwd:
print("\033[32;1mUser has passed authentication!\033[0m")
#被装饰的函数中有返回值,装饰器中传入的参数函数要有返回值
res = func(*args, **kwargs) #from home
print("-----after authentication---")
return res
else:
exit("\033[31;1mInvalid username or password!\033[0m")
elif auth_type == "ldap":
print("ldap....")
return wrapper
return outer_wrapper
def index():
print("welcome to index page!")
@auth(auth_type="local") #利用本地登录 home = wrapper()
def home():
print("welcome to home page!")
return "from home"
@auth(auth_type="ldap") #利用远程的ldap登录
def bbs():
print("welcome to bbs page!")
#函数调用
index()
print(home()) #wrapper()
bbs()
运行结果:
希望本文所述对大家Python程序设计有所帮助。
来源:https://blog.csdn.net/loveliuzz/article/details/77853346
猜你喜欢
- 首次安装、运行MySQL时,你可能会遇到一些错误,使MySQL服务器不能启动。本节的目的是帮助你诊断并纠正这些错误。解决服务器问题时你的第一
- 首先说说什么是内存泄露,在一个进程中,如果某一块内存无法访问,且直到进程结束为止也无法释放,那么就发生了内存泄露。通常这种情况发生在C++之
- GetRows 方法 将 Recordset 对象的多个记录复制到数组中。 语法 代码如下: array = recordset.GetR
- 第二次遇到FF下正则兼容性问题( 第一次是关于"g"全局标志的,现在网上已有很多相关文章介绍)。以下正则在FF和IE下的
- table估计每个跟web打过交道的人都会经常接触到,跟js结合能做出很多不错的体验。这里打算结合js做一个系列,包括一些操作和效果,虽然现
- 主要要文件有:Index.html 实现功能,一个文本框,输入内容并实现提示 search.asp 查询功能,让文本框输入的内容在
- 一般情况下,访问或设置剪贴板,IE 只需使用 window.clipboardData 的 getData 或 setData 方法即可。M
- div和span、relative和absolute、display和visibility是很容易混淆和弄错的HTML标签与CSS属性,简单
- 在进行浮动布局时,大多数人都深知,在必要的地方进行浮动清理:<div style="clear:both;">
- 代码如下:Function splitx(strs1 As String, strs2 A
- PHP PDO 大对象 (LOBs)应用程序在某一时刻,可能需要在数据库中存储"大"数据。"大"通常
- 游标(Cursor)是处理数据的一种方法,为了查看或者处理结果集中的数据,游标提供了在结果集中一次以行或者多行前进或向后浏览数据的能力。我们
- 学习目的: 学习ADO.NET用法,并如何用DataRearder读取数据 今天练习数据库的最基本用法,如何打开数据库。首先在网站设置文件w
- 实际工作经历中,免不了有时候需要连接数据库进行问题排查分析的场景,之前一直习惯通过 mysql -uxxx -hxxxx -P1234 ..
- master库对于SQLServer来说,是很重要的系统数据库,保存着所有Sqlserver的用户信息、数据库信息等,当数据库崩溃时,mas
- clipboardData 对象提供了对于预定义的剪贴板格式的访问,以便在编辑操作中使用。成员表方法 描述 clearData 通过 dat
- 出自: 编程中国 http://www.bc-cn.net作者: 天涯听雨 &nbs
- 一个项目开发完毕后总有一种想法,就是生成可执行文件,总不能一直用python xxx执行吧。以下操作同时适用于windows和Linux下的
- 注意:安装时要保证Oracle安装目录不能带有中文字符(如果第一次安装出现“加载数据库错误areasQueries”的错误,一般是因为Ora
- SQL Server数据库查询速度慢的原因有很多,常见的有以下几种:1、没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷)