python :编写装饰器
简单装饰器
def log_time(func): # 此函数的作用时接受被修饰的函数的引用test,然后被内部函数使用
def make_decorater():
print('现在开始装饰')
func()
print('现在结束装饰')
return make_decorater # log_time()被调用后,运行此函数返回make_decorater()函数的引用make_decorater @log_time # 此行代码等同于,test=log_time(test)=make_decorater
def test():
print('我是被装饰的函数')
test() # test()=make_decorater() 调试结果
D:\pycharm_project\装饰器\venv\Scripts\python.exe D:/pycharm_project/装饰器/venv/装饰器.py
现在开始装饰
我是被装饰的函数
现在结束装饰 Process finished with exit code
带参的
形参
def log_time(func):
def make_decorater(*args,**kwargs): # 接受调用语句的实参,在下面传递给被装饰函数(原函数)
print('现在开始装饰')
test_func = func(*args,**kwargs) # 如果在这里return,则下面的代码无法执行,所以引用并在下面返回
print('现在结束装饰')
return test_func # 因为被装饰函数里有return,所以需要给调用语句(test())一个返回,又因为test_func = func(*args,**kwargs)已经调用了被装饰函数,这里就不用带()调用了,区别在于运行顺序的不同。
return make_decorater @log_time
def test(num):
print('我是被装饰的函数')
return num+ a = test() # test()=make_decorater()
print(a)
传入注解的参数注意!有两个return,log_time函数也要return
def get_parameter(*args,**kwargs): # 工厂函数,用来接受@get_parameter('index.html/')的'index.html/'
def log_time(func):
def make_decorater():
print(args,kwargs)
print('现在开始装饰')
func()
print('现在结束装饰')
return make_decorater
return log_time
@get_parameter('index.html/')
def test():
print('我是被装饰的函数')
# return num+
test() # test()=make_decorater()
D:\pycharm_project\装饰器\venv\Scripts\python.exe D:/pycharm_project/装饰器/venv/装饰器.py
('index.html/',) {}
现在开始装饰
我是被装饰的函数
现在结束装饰
Process finished with exit code
装饰器:在不修改函数源代码的基础上,添加函数功能
一个简单的装饰器
|
1
2
3
4
5
6
7
8
9
10
11
|
def log_time(func): # 此函数的作用时接受被修饰的函数的引用test,然后被内部函数使用 def make_decorater(): print('现在开始装饰') func() print('现在结束装饰') return make_decorater # log_time()被调用后,运行此函数返回make_decorater()函数的引用make_decorater@log_time # 此行代码等同于,test=log_time(test)=make_decoraterdef test(): print('我是被装饰的函数')test() # test()=make_decorater() |
|
1
2
3
4
5
6
|
D:\pycharm_project\装饰器\venv\Scripts\python.exe D:/pycharm_project/装饰器/venv/装饰器.py现在开始装饰我是被装饰的函数现在结束装饰Process finished with exit code 0 |
当被装饰的函数有形参时
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
def log_time(func): def make_decorater(*args,**kwargs): # 接受调用语句的实参,在下面传递给被装饰函数(原函数) print('现在开始装饰') test_func = func(*args,**kwargs) # 如果在这里return,则下面的代码无法执行,所以引用并在下面返回 print('现在结束装饰') return test_func # 因为被装饰函数里有return,所以需要给调用语句(test(2))一个返回,又因为test_func = func(*args,**kwargs)已经调用了被装饰函数,这里就不用带()调用了,区别在于运行顺序的不同。 return make_decorater@log_timedef test(num): print('我是被装饰的函数') return num+1a = test(2) # test(2)=make_decorater(2)print(a) |
|
1
2
3
4
5
6
7
|
D:\pycharm_project\装饰器\venv\Scripts\python.exe D:/pycharm_project/装饰器/venv/装饰器.py现在开始装饰我是被装饰的函数现在结束装饰3Process finished with exit code 0 |
当@装饰器后有参数时
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
def get_parameter(*args,**kwargs): # 工厂函数,用来接受@get_parameter('index.html/')的'index.html/' def log_time(func): def make_decorater(): print(args,kwargs) print('现在开始装饰') func() print('现在结束装饰') return make_decorater return log_time@get_parameter('index.html/')def test(): print('我是被装饰的函数') # return num+1test() # test()=make_decorater() |
|
1
2
3
4
5
6
7
|
D:\pycharm_project\装饰器\venv\Scripts\python.exe D:/pycharm_project/装饰器/venv/装饰器.py('index.html/',) {}现在开始装饰我是被装饰的函数现在结束装饰Process finished with exit code 0 |
python :编写装饰器的更多相关文章
- 编写装饰器实现python请求错误重试功能
在做接口自动化测试的时候,总会遇到,因连接超时等错误导致,接口脚本失败. 官方给出的方法: max_retries=5 出错重试5次注意的是,这个只对DNS,连接错误进行重试. from reques ...
- python基础——装饰器
python基础——装饰器 由于函数也是一个对象,而且函数对象可以被赋值给变量,所以,通过变量也能调用该函数. >>> def now(): ... print('2015-3-25 ...
- 两个实用的Python的装饰器
两个实用的Python的装饰器 超时函数 这个函数的作用在于可以给任意可能会hang住的函数添加超时功能,这个功能在编写外部API调用 .网络爬虫.数据库查询的时候特别有用 timeout装饰器的代码 ...
- Python函数——装饰器
前言 给下面的函数加上运行时间 def fib(n): a, b = 0, 1 for i in range(n): print(b) a, b = b, a+b return b a = fib(5 ...
- Python学习---装饰器的学习1210
装饰器的基础 学习前提: 作用域 + 函数的理解 + 闭包 [学习,理解] 代码编写原则: 对修改开放对扩展开放 装饰器本质上是一个函数,该函数用来处理其他函数,它可以让其他函数在不需要修改代码的前 ...
- Python设计模式-装饰器模式
装饰器模式 装饰器模式,动态地给一个对象添加一些额外的职责,就增加功能来说,装饰器模式比生成子类更为灵活. 代码示例 #coding:utf-8 #装饰器模式 class Beverage(): ...
- Python的装饰器实例用法小结
这篇文章主要介绍了Python装饰器用法,结合实例形式总结分析了Python常用装饰器的概念.功能.使用方法及相关注意事项 一.装饰器是什么 python的装饰器本质上是一个Python函数,它可以让 ...
- 第五章 Python之装饰器
函数对象 函数是第一类对象:即函数可以当作数据传递 #可以被引用,可以被当作参数传递,返回值可以是函数,可以当作容器类型的元素 #引用 def func(x,y): print(x,y) f=func ...
- 进阶Python:装饰器 全面详解
进阶Python:装饰器 前言 前段时间我发了一篇讲解Python调试工具PySnooper的文章,在那篇文章开始一部分我简单的介绍了一下装饰器,文章发出之后有几位同学说"终于了解装饰器的用 ...
- Python函数装饰器原理与用法详解《摘》
本文实例讲述了Python函数装饰器原理与用法.分享给大家供大家参考,具体如下: 装饰器本质上是一个函数,该函数用来处理其他函数,它可以让其他函数在不需要修改代码的前提下增加额外的功能,装饰器的返回值 ...
随机推荐
- mysql——索引失效
索引失效的几种情况:https://www.jianshu.com/p/9c9a0057221f
- sqlt 之 分析 DB upgrade 导致SQL 性能下降 的方法 xplore
https://blog.csdn.net/lukeUnique/article/details/79331779 https://mauro-pagano.com/2014/10/27/when-t ...
- 百度webuploader 上传演示例子
前端代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="baiduWebU ...
- python 合并重叠数据
- 11073 最热门的K个搜索串
11073 最热门的K个搜索串时间限制:350MS 内存限制:65535K提交次数:0 通过次数:0 题型: 编程题 语言: G++;GCC;VCDescription大家都非常喜欢而习惯用baidu ...
- Murano Weekly Meeting 2015.10.06
Meeting time: 2015.October.6th 1:00~2:00 Chairperson: Kirill Zaitsev, from Mirantis Meeting summar ...
- 虚拟机扩容(/dev/mapper/centos-root 空间不足)
1:.首先查看我们的根分区大小是多少 df -h 文件系统 类型 容量 已用 可用 已用% 挂载点 /dev/mapper/centos-root xfs ...
- stm32中断优先级快速入门
1.基本概念 STM32(Cortex-M3架构)中有两个优先级的概念--抢占式优先级和响应优先级.有人把响应优先级称作'亚优先级'或'副优先级',每个中断源都需要被指定这两种优先级. 具有高抢占式优 ...
- PHP高级分离术
<!--高级分离术--> <table border='1px'> <?php foreach($data as $key=>$value){ ...
- HTML--备忘点
1.文档内的链接