python中装饰器使用
装饰器是对已有的模块进行装饰(添加新功能)的函数。
现有一段代码:
import time
def func1():
time.sleep(3)
print("in the func1")
def func2():
time.sleep(2)
print("in the func2")
func1()
func2()
现在需要增加func1和func2的功能,计算段代码的运行时间。
思路1:修改函数内的代码:
import time
def func1():
start_time = time.time()
time.sleep(3)
print("in the func1")
stop_time = time.time()
print("this program run %ss"%(stop_time-start_time))
def func2():
start_time = time.time()
time.sleep(2)
print("in the func2")
stop_time = time.time()
print("this program run %ss"%(stop_time-start_time))
func1()
func2()
每个函数都添加了红色的代码段,如果需要改的func较多时,该方法明显不适用。并且违反了为函数添加附加功能时的原则1:不能修改被装饰函数的源代码。
思路2:增加新函数:
import time
def func1():
time.sleep(3)
print("in the func1")
def func2():
time.sleep(2)
print("in the func2")
def func_new1():
start_time = time.time()
func1()
stop_time =time.time()
print("this program run %ss"%(stop_time-start_time))
def func_new2():
start_time = time.time()
func2()
stop_time =time.time()
print("this program run %ss"%(stop_time-start_time))
func_new1()
func_new2()
该方法增加了新函数,将原有函数引用至新函数中。但问题又来了,需要改主程序中对原有函数的引用方法,违反了并且违反了为函数添加附加功能时的原则2:不能修改被装饰函数的调用方式。
此刻,我们需要新的知识储备来完善对装饰器的理解
1.函数的嵌套
2.高阶函数
3.函数——变量的关系
高阶函数+嵌套函数===》装饰器
插入名词的解释!!
高阶函数:满足下列原则之一的就是高阶函数:
a.把一个函数名当作实参传给另一个函数的函数。
import time
def bar():
time.sleep(3)
print("in the bar")
def test(func):
start_time = time.time()
func()
stop_time = time.time()
print("the func run time is %s"%(stop_time-start_time))
test(bar)
在此函数中,bar作为实参被传递给形参func,test(bar)。通过test()函数为bar()增加了新功能,且没有改变bar()的源代码。但改变了原有bar()的调用方式。
实现了不修改被装饰函数的源代码情况下为其添加新功能的目的。
b.返回值中包含函数名
import time
def bar():
time.sleep(3)
print("in the bar")
def test(func):
print(func)
print()
return func
bar = test(bar)
bar()
在此代码中,原有函数bar(),然后bar作为函数名被传给新增函数test(),随后又作为该函数的返回值。相当与给bar()增加了新的功能(显示bar()的内存地址),但没有改变bar()的调用方式。
嵌套函数:在函数体内声明的函数(必须是声明,不能是调用)
def test1():
print("in the test1")
def test2():
print("in the test2")
test2()
test1()
test2()是在test1()中声明的函数
将高阶函数与嵌套函数结合,
import time
def timmer(func):
def deco():
start_time = time.time()
func()
stop_time = time.time()
print("the func run time is %ss"%(stop_time-start_time))
return deco
@timmer #等于在func1被调用前加上func1=timmer(func1)
def func1():
time.sleep(3)
print("in the func1")
@timmer
def func2():
time.sleep(2)
print("in the func2")
func1()
func2()
其中@timmer = func1= timmer(func1),就是在被装饰的函数前加上装饰器的名称。在没有改变被装饰代码的源代码及调用方式的情况下增加了其功能。
python中装饰器使用的更多相关文章
- 8.Python中装饰器是什么?
Python中装饰器是什么? A Python decorator is a specific change that we make in Python syntax to alter functi ...
- python中装饰器的原理以及实现,
python版本 3.6 1.python的装饰器说白了就是闭包函数的一种应用场景,在运用的时候我们遵循 #开放封闭原则:对修改封闭,对拓展开放 2.什么是装饰器 #装饰他人的器具,本身可以是任意可调 ...
- python中装饰器修复技术
python装饰器@wraps作用-修复被装饰后的函数名等属性的改变 Python装饰器(decorator)在实现的时候,被装饰后的函数其实已经是另外一个函数了(函数名等函数属性会发生改变), 为了 ...
- python中装饰器的执行细节
本文代码借用 廖雪峰的python教程(官网:http://www.liaoxuefeng.com/) 不了解装饰器的可以先看教程 直接上带参数装饰器的代码 def log(text): def de ...
- Python中装饰器(转)
本文由 伯乐在线 - 7even 翻译,艾凌风 校稿.未经许可,禁止转载!英文出处:Simeon Franklin.欢迎加入翻译组. 好吧,我标题党了.作为 Python 教师,我发现理解装饰器是学生 ...
- python中装饰器(语法糖)概念
“”“” 什么是装饰器? """ 还是通过一个例子来慢慢说明 先看下面的例子 def func_1(x): return x*2 def fun_2(x): return ...
- Python核心技术与实战——十四|Python中装饰器的使用
我在以前的帖子里讲了装饰器的用法,这里我们来具体讲一讲Python中的装饰器,这里,我们从前面讲的函数,闭包为切入点,引出装饰器的概念.表达和基本使用方法.其次,我们结合一些实际工程中的例子,以便能再 ...
- Python中装饰器的用法
定义: 装饰器本身就是一个函数 为其他函数提供附加功能 不改变源代码 不改变原调用方式 装饰器=高阶函数+嵌套函数 知识点: 函数本身就是一个变量(意味着可以被复制给一个变量:test=test(1) ...
- python中装饰器的原理
装饰器这玩意挺有用,当时感觉各种绕,现在终于绕明白了,俺滴个大爷,还是要慢慢思考才能买明白各种的真谛,没事就来绕一绕 def outer(func): def inner(): print(" ...
随机推荐
- 常用官方php版本下载链接
windows 版本 https://windows.php.net/downloads/releases/archives/ https://windows.php.net/download/#ph ...
- mysql 连表查询
现有tablea: tableb: ...
- metasploit framework(十三):FTP扫描
设置目标IP 查看是否支持匿名登录 获取到版本信息 使用ftp登录模块 搜索刚才获取到的版本信息,找到一个针对这个版本的漏洞利用代码
- 五、Singleton 单例模式
需求:保证对象只创建一次 说明: 分为懒汉式.饿汉式,通过是否一开始就创建静态对象.饿汉式需要考虑线程并发的安全 懒汉式: public class Singleton { private stati ...
- WDlinux 修改后台默认8080端口的方法
修改8080端口正确方法 新版本: 方法一: apache sed -i 's/8080/8088/' /www/wdlinux/wdapache/conf/httpd.conf 然后记得修改防火墙i ...
- Django 自定义 过滤器和模板标签
代码布局(自定义的代码,放在哪里) 二种方式:1. 某个app特有的 -app 目录下,templatetags 文件夹 ** 必需是这个名称的包(目录中有__init__.py文件) -再到 ...
- centos7 脚本搭建SVN
#!/usr/bin/env bash #安装软件 HTTP 和 SVN软件 yum install -y httpd subversion mod_dav_svn #创建库文件夹并更改文件夹权限 m ...
- NumPy 从已有的数组创建数组
NumPy 从已有的数组创建数组 本章节我们将学习如何从已有的数组创建数组. numpy.asarray numpy.asarray 类似 numpy.array,但 numpy.asarray 只有 ...
- 解决node-pre-gyp install --fallback-to-build 卡住不动
一般是因为需要下载国外的包,要么连VPN,要么使用淘宝的镜像: 使用cnpm: npm install -g cnpm --registry=https://registry.npm.taobao.o ...
- ztree-持续更新中
版本v3快速入门: 1,官网下载https://gitee.com/zTree/zTree_v3 2,zTree-zTree_v3-master\zTree_v3下复制css和js文件夹到项目下 3, ...