Python高级笔记(十一)装饰器【面试】
1. 需求
开发封闭原则:虽然在这个原则是用的面向对象开发,但是也适用于函数式编程,简单来说,它规定已经实现的功能代码不允许被修改,但可以被拓展,即:
- 封闭:已实现的功能代码块
- 开发:对拓展开发
2. 使用装饰器
2.1 未使用装饰器(原理)
def w1(func):
def inner():
# 验证1
# 验证2
# 验证3
print("----正在验证权限----")
func()
return inner def f1():
print("---f1----") def f2():
print("---f2----") f1 = w1(f1) f1()
----正在验证权限----
---f1----
2.2 使用装饰器
def w1(func):
def inner():
# 验证1
# 验证2
# 验证3
print("----正在验证权限----")
func()
return inner @w1
def f1():
print("---f1----") @w1
def f2():
print("---f2----") f1()
f2()
----正在验证权限----
---f1----
----正在验证权限----
---f2----
3. 再议装饰器
# 定义函数:完成包裹数据
def makeBold(fn):
def wrapped():
print("-----1------")
return "<b>" + fn() + "</b>"
return wrapped # 定义函数:完成包裹数据
def makeItalic(fn):
def wrapped():
print("-----2------")
return "<i>" + fn() + "</i>"
return wrapped @makeBold
@makeItalic
def test3():
print("-----3-----")
return "hello world" s = test3()
print(s)
-----1------
-----2------
-----3-----
<b><i>hello world</i></b>
4. 装饰器执行时间
@w1
def f1():
print("---f1----")
只要Python解释器执行到了这个代码 ,那么就会自动的进行装饰,而不是等到调用的时候才装饰
5. 装饰器对有参数、无参数函数进行装饰
5.1 无参数
def func(functionName):
print("-----func----1-----")
def func_in():
print("---func_in----1----")
functionName()
print("---func_in----2----") print("-----func----2-----")
return func_in @func
def test():
print("----test------") test()
-----func----1-----
-----func----2-----
---func_in----1----
----test------
---func_in----2----
5.2 函数有参数
def func(functionName):
print("-----func----1-----")
def func_in(aa, bb):
print("---func_in----1----")
functionName(aa, bb)
print("---func_in----2----") print("-----func----2-----")
return func_in @func
def test(a, b):
print("----test---a=%d---b=%d---" %(a, b)) test(11, 22)
-----func----1-----
-----func----2-----
---func_in----1----
----test---a=11---b=22---
---func_in----2----
5.3 函数有不定参数
def func(functionName):
print("-----func----1-----")
def func_in(*args, **kargs):
print("---func_in----1----")
functionName(*args, **kargs)
print("---func_in----2----") print("-----func----2-----")
return func_in @func
def test(a, b, c):
print("----test---a=%d---b=%d---c=%d-------" %(a, b, c)) test(11, 22, 33)
-----func----1-----
-----func----2-----
---func_in----1----
----test---a=11---b=22---c=33-------
---func_in----2----
5.4 函数有返回值
def func(functionName):
print("-----func----1-----")
def func_in(*args, **kargs):
print("---func_in----1----")
res = functionName(*args, **kargs)
print("---func_in----2----")
return res print("-----func----2-----")
return func_in @func
def test(a, b, c):
print("----test---a=%d---b=%d---c=%d-------" %(a, b, c))
return "hahaha" res = test(11, 22, 33)
print("res: ", res)
-----func----1-----
-----func----2-----
---func_in----1----
----test---a=11---b=22---c=33-------
---func_in----2----
res: hahaha
6. 通用的装饰器
核心代码:
def func(functionName):
print("-----func----1-----")
def func_in(*args, **kargs):
print("---func_in----1----")
res = functionName(*args, **kargs)
print("---func_in----2----")
return res print("-----func----2-----")
return func_in
验证:
def func(functionName):
print("-----func----1-----")
def func_in(*args, **kargs):
print("---func_in----1----")
res = functionName(*args, **kargs)
print("---func_in----2----")
return res print("-----func----2-----")
return func_in @func
def test(a, b, c):
print("----test---a=%d---b=%d---c=%d-------" %(a, b, c))
return "hahaha" @func
def test2(a):
print("-----test2---%d---"%a) res = test(11, 22, 33)
print("res: ", res) test2(3)
-----func----1-----
-----func----2-----
-----func----1-----
-----func----2-----
---func_in----1----
----test---a=11---b=22---c=33-------
---func_in----2----
res: hahaha
---func_in----1----
-----test2---3---
---func_in----2----
7. 装饰器有参数
from time import ctime, sleep def timefun_arg(pre="hello"):
def timefun(func):
def wrappedfunc():
print("%s called at %s %s" %(func.__name__, ctime(), pre))
return func()
return wrappedfunc
return timefun @timefun_arg("douzi")
def foo():
print("I am foo") @timefun_arg("python")
def too():
print("I am too") foo()
sleep(2)
too()
foo called at Thu May 9 00:57:58 2019 douzi
I am foo
too called at Thu May 9 00:58:00 2019 python
I am too
8. 综合
from time import ctime, sleep def timefun_arg(pre="hello"):
print("fun_1")
def timefun(func):
print("fun_in_1")
def wrappedfunc(*args, **kwargs):
print("%s called at %s %s" %(func.__name__, ctime(), pre))
return func(*args, **kwargs)
print("fun_in_2")
return wrappedfunc
print("fun_2")
return timefun @timefun_arg("douzi")
def foo(fname):
print(fname, "I am foo") @timefun_arg("python")
def too():
print("I am too") foo("fname ")
sleep(2)
too()
最新文章
- 折腾一两天,终于学会使用grunt压缩合并混淆JS脚本,小激动,特意记录一下+spm一点意外收获
- RequireJS 基础(一)
- python从Microsoft Excel文件中导入数据
- PHP 常用正则汇总
- RC4加密解密算法
- We7——很有意思的一个开源CMS
- tomcat使用所遇到的问题
- lua 的 break
- javascript移动端禁止页面滑动的解决方案
- BZOJ.3351.[IOI2009]Regions(根号分治 差分)
- 更改Nginx网站根目录以及导致的403 forbidden问题解决
- [NOIp2009普及组]细胞分裂
- 《objective-c基础教程》学习笔记(三)—— 从结构体到面向对象
- 【leetcode】58-LengthofLastWord
- Linux移植之auto.conf、autoconf.h、Mach-types.h的生成过程简析
- ubuntu下wps无法使用搜狗输入法输入中文
- PostgreSQL存储过程(4)-return语句
- mybatis项目启动报错 The content of element type ";resultMap"; must match ";(constructor?,id*,result*,association*,collection*,discriminator?)";.
- 正则表达式&;自定义异常 典型案例
- duilib 修复CTreeViewUI控件动态添加子控件时,对是否显示判断不足的bug