装饰器是对已有的模块进行装饰(添加新功能)的函数。

现有一段代码:

 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中装饰器使用的更多相关文章

  1. 8.Python中装饰器是什么?

    Python中装饰器是什么? A Python decorator is a specific change that we make in Python syntax to alter functi ...

  2. python中装饰器的原理以及实现,

    python版本 3.6 1.python的装饰器说白了就是闭包函数的一种应用场景,在运用的时候我们遵循 #开放封闭原则:对修改封闭,对拓展开放 2.什么是装饰器 #装饰他人的器具,本身可以是任意可调 ...

  3. python中装饰器修复技术

    python装饰器@wraps作用-修复被装饰后的函数名等属性的改变 Python装饰器(decorator)在实现的时候,被装饰后的函数其实已经是另外一个函数了(函数名等函数属性会发生改变), 为了 ...

  4. python中装饰器的执行细节

    本文代码借用 廖雪峰的python教程(官网:http://www.liaoxuefeng.com/) 不了解装饰器的可以先看教程 直接上带参数装饰器的代码 def log(text): def de ...

  5. Python中装饰器(转)

    本文由 伯乐在线 - 7even 翻译,艾凌风 校稿.未经许可,禁止转载!英文出处:Simeon Franklin.欢迎加入翻译组. 好吧,我标题党了.作为 Python 教师,我发现理解装饰器是学生 ...

  6. python中装饰器(语法糖)概念

    “”“” 什么是装饰器? """ 还是通过一个例子来慢慢说明 先看下面的例子 def func_1(x): return x*2 def fun_2(x): return ...

  7. Python核心技术与实战——十四|Python中装饰器的使用

    我在以前的帖子里讲了装饰器的用法,这里我们来具体讲一讲Python中的装饰器,这里,我们从前面讲的函数,闭包为切入点,引出装饰器的概念.表达和基本使用方法.其次,我们结合一些实际工程中的例子,以便能再 ...

  8. Python中装饰器的用法

    定义: 装饰器本身就是一个函数 为其他函数提供附加功能 不改变源代码 不改变原调用方式 装饰器=高阶函数+嵌套函数 知识点: 函数本身就是一个变量(意味着可以被复制给一个变量:test=test(1) ...

  9. python中装饰器的原理

    装饰器这玩意挺有用,当时感觉各种绕,现在终于绕明白了,俺滴个大爷,还是要慢慢思考才能买明白各种的真谛,没事就来绕一绕 def outer(func): def inner(): print(" ...

随机推荐

  1. pipy国内镜像的网址

    pipy国内镜像目前有:豆瓣 http://pypi.douban.com/simple/阿里云 http://mirrors.aliyun.com/pypi/simple/中国科技大学 https: ...

  2. The CHAR and VARCHAR Types

    [The CHAR and VARCHAR Types] The CHAR and VARCHAR types are declared with a length that indicates th ...

  3. appium的兼容问题

    appium 和Android7.0的兼容问题 标签(空格分隔): appium 随着Android系统的不断的迭代更新,目前Android系统都已经更新到9.0系统了,有些小伙伴appium版本还是 ...

  4. .sh_history文件的管理机制

    来源:http://www.aixchina.net/Article/27258 字数 1056阅读 4365评论 1赞 0 内容提要: .sh_history是在ksh中用于存储用户在shell中输 ...

  5. 98. Validate Binary Search Tree (Tree; DFS)

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  6. 数据存储 共享参数 SharedPreferences

    先要声明文件名和操作方式,第一个参数:文件名为"share.xml",第二个参数:私有模式SharedPreferences shared = getSharedPreferenc ...

  7. MySQL千万级数据分区存储及查询优化

    作为传统的关系型数据库,MySQL因其体积小.速度快.总体拥有成本低受到中小企业的热捧,但是对于大数据量(百万级以上)的操作显得有些力不从心,这里我结合之前开发的一个web系统来介绍一下MySQL数据 ...

  8. 《学习OpenCV(中文版)》

    <模式识别中文版(希)西奥多里蒂斯> <学习OpenCV(中文版)> 矩阵计算 英文版 第四版 Matrix Computations OpenCV 3.x with Pyth ...

  9. Codeforces Beta Round #32 (Div. 2, Codeforces format)

    Codeforces Beta Round #32 (Div. 2, Codeforces format) http://codeforces.com/contest/32 A #include< ...

  10. 高盛oa

    一道题根本不会,抄答案过了.一道自己写,莫名其妙出现了不会的bug.最后交了暴力解,过了5/7.估计要跪. 总结: 缺点:做过的不熟练.没做过的题不会.一个陌生的小bug也de不出来. 措施:多总结还 ...