python里使用带参数的装饰器

来源:互联网 发布:上帝已死 知乎 编辑:程序博客网 时间:2024/05/21 20:24
在前面一文《python里为什么需要使用装饰器(decorator)》里,我们学习了为什么需要装饰器,知道装饰器就是为了不修改原来函数的代码,又达到增加功能的作用。其实为了装饰器更通用化,那么装饰器是否也可以带参数呢?其实是可以的,这样更加通用化了,达到共享极点。在前面也学习《为什么要使用闭包(closures)》一文,知道参数可以嵌套函数里实现隐藏,并且实现全局参数的功能,与函数一起绑定。因此只需要结合这两个知识点,就可以产生通用带参数的装饰器了。原来不带参数的代码如下:
#python 3.6#定义一个新的函数def printStar(func):    def f():        print('*************************************')        return func()    return f@printStardef add():        return 1 + 1@printStardef sub():        return 2 -1    print(add())print(sub())

现在再对装饰器函数进一步修改,再嵌套一层函数,实现闭包的功能,代码就修改如下:
#python 3.6#定义一个新的函数def title(show = ''):    def printStar(func):        def f():            print(show,'*************************************')            return func()        return f    return printStar@title('add')def add():        return 1 + 1@title('sub')def sub():        return 2 -1    print(add())print(sub())

输出如下:

add *************************************
2
sub *************************************
1

经过带参数的装饰器的修改,这时控制输出的内容,就更方便了。可以根据不同的函数名称来进行修改输出提示。

前面发现被装饰的函数都没有带参数,这是为了简单起见,现在来学习一下如果被装饰的函数也参数,怎么样修改呢?
可以看下面的代码:

#python 3.6#定义一个新的函数def title(show = ''):    def printStar(func):        def f(a, b):            print(show,'*************************************')            return func(a, b)        return f    return printStar@title('add')def add(a, b):        return a + b@title('sub')def sub(a, b):        return a - b    print(add(1, 1))print(sub(2, 1))
由两段代码相比较,可以发现函数传送参数是修改def f(a, b),就是第三层的函数。

Python游戏开发入门

http://edu.csdn.net/course/detail/5690

你也能动手修改C编译器

http://edu.csdn.net/course/detail/5582

纸牌游戏开发

http://edu.csdn.net/course/detail/5538 

五子棋游戏开发

http://edu.csdn.net/course/detail/5487
RPG游戏从入门到精通
http://edu.csdn.net/course/detail/5246
WiX安装工具的使用
http://edu.csdn.net/course/detail/5207
俄罗斯方块游戏开发
http://edu.csdn.net/course/detail/5110
boost库入门基础
http://edu.csdn.net/course/detail/5029
Arduino入门基础
http://edu.csdn.net/course/detail/4931
Unity5.x游戏基础入门
http://edu.csdn.net/course/detail/4810
TensorFlow API攻略
http://edu.csdn.net/course/detail/4495
TensorFlow入门基本教程
http://edu.csdn.net/course/detail/4369
C++标准模板库从入门到精通 
http://edu.csdn.net/course/detail/3324
跟老菜鸟学C++
http://edu.csdn.net/course/detail/2901
跟老菜鸟学python
http://edu.csdn.net/course/detail/2592
在VC2015里学会使用tinyxml库
http://edu.csdn.net/course/detail/2590
在Windows下SVN的版本管理与实战 
http://edu.csdn.net/course/detail/2579
Visual Studio 2015开发C++程序的基本使用 
http://edu.csdn.net/course/detail/2570
在VC2015里使用protobuf协议
http://edu.csdn.net/course/detail/2582
在VC2015里学会使用MySQL数据库
http://edu.csdn.net/course/detail/2672