装饰器学习
来源:互联网 发布:广州凯申物流 知乎 编辑:程序博客网 时间:2024/05/22 06:37
定义:本质是函数,(装饰其他函数)就是为其他函数添加附加功能
原则:1.不能修改被装饰的函数的源代码
2.不能修改被装饰的函数的调用方式
实现装饰器知识储备:
1.函数即“变量”,函数在调用前需要先声明
2. 高阶函数
a:把一个函数名当作实参传递给另外一个函数(在不修改被装饰函数情况下为其增加功能)
def test1():
print('in the test1')
def test2(func): # func = test1
res = func() # func() = test1()
return res
test2(test1)
结果:in the test1
b:返回值中包含函数名(不修改函数的调用方式)
def test1():
print('in the test1')
def test2(func): # func = test1
return func
test1 = test2(test1)
test1()
结果:in the test1
3. 嵌套函数:在一个函数内声明一个函数
def test1():
def test2():
print("in the test2")
test2()
test1()
结果:in the test2
内嵌函数的作用在函数内部,不可以在外部进行调用,如下面则是错误的:
def test1():
def test2():
print("in the test2")
test1()
test2() # 该函数作用域在函数test1内部,超出该范围则会报错
结果:NameError: name 'test2' is not defined
如果在函数内部有和函数外部同名变量,优先访问内部变量
def test1():
x = 1
def test2():
x = 2
def test3():
x = 3
print(x)
test3()
test2()
test1()
结果:3
高级函数 + 嵌套函数 =》装饰器
1. 无参数
Import time
def decorartor(func):
def wrapper():
start_time = time.time()
func()
stop_time = time.time()
print("the func run time is % s" % (stop_time - start_time))
return wrapper
def test1():
time.sleep(3)
print('in the test1')
print(decorartor(test1))
结果:<function decorartor.<locals>.wrapper at 0x000000000270AA60>
上面decorartor(test1) 得到的是 wrapper函数体存放的地址空间,想要进行执行并的到结果,可以如下操作:
test1 = decorartor(test1)
test1()
# 或decorartor(test1)()
结果:
in the test1
the func run time is 3.0137999057769775
2.固定参数
import time
def decorartor(func):
def wrapper(n):
start_time = time.time()
func(n)
stop_time = time.time()
print("the func run time is % s" % (stop_time - start_time))
return wrapper
def test1(n):
time.sleep(3)
print('in the test1 n is %s' % n)
#decorartor(test1)(10)
test = decorartor(test1)
test(10)
结果:
in the test1 n is 10
the func run time is 3.0138003826141357
3. 不定参数
import time
def decorartor(func):
def wrapper(*args, **kwargs):
start_time = time.time()
func(*args, **kwargs)
stop_time = time.time()
print("the func run time is % s" % (stop_time - start_time))
return wrapper
def test1(n):
time.sleep(3)
print('in the test1 n is %s' % n)
def test2(n, m):
time.sleep(3)
print('in the test1 n is %s, m is %s' % (n, m))
decorartor(test1)(10)
decorartor(test2)(10, 100)
结果:
in the test1 n is 10
the func run time is 3.012200117111206
in the test1 n is 10, m is 100
the func run time is 3.000199794769287
4. 无参装饰器
import time
def decorartor(func):
def wrapper(*args, **kwargs):
start_time = time.time()
func(*args, **kwargs)
stop_time = time.time()
print("the func run time is % s" % (stop_time - start_time))
return wrapper
@decorartor # 相当于 test1 = decorartor(test1)
def test1():
time.sleep(3)
print('in the test1 ')
@decorartor # 相当于 test2 = decorartor(test2)
def test2(n, m):
time.sleep(3)
print('in the test1 n is %s, m is %s' % (n, m))
test1()
test2(10, 100)
结果:
in the test1
the func run time is 3.0
in the test1 n is 10, m is 100
the func run time is 3.0
5. 有参装饰器
import time
def timer(timeout = 0):
def decorartor(func):
def wrapper(*args, **kwargs):
start_time = time.time()
func(*args, **kwargs)
stop_time = time.time()
print("the func run time is % s" % (stop_time - start_time))
if (stop_time - start_time) > timeout:
print("程序运行超时")
return wrapper
return decorartor
@timer(2) # 相当于 test1 = decorartor(test1)
def test1():
time.sleep(3)
print('in the test1')
@timer(5) # 相当于 test2 = decorartor(test2)
def test2(n, m):
time.sleep(3)
print('in the test1 n is %s, m is %s' % (n, m))
test1()
test2(10, 100)
结果:
in the test1
the func run time is 3.0
程序运行超时
in the test1 n is 10, m is 100
the func run time is 3.0
- Python装饰器学习
- Python装饰器学习
- Python装饰器学习
- Python装饰器学习
- python 装饰器学习
- Python装饰器学习
- 装饰器学习
- python 装饰器学习
- Python装饰器学习
- Python装饰器学习
- Python装饰器学习
- python装饰器学习
- Python装饰器学习
- python学习-装饰器
- Python装饰器学习
- Python装饰器学习
- python 装饰器学习
- Python装饰器学习
- Caffe2 Tutorials Overview(一)
- NETGEAR WNDR3800 刷机支持ipv6 部署本地git服务器
- Java+Selenium3方法篇18-获取当前页的源码
- TCP建立/关闭连接时握手过程中的状态情况
- Object-c的bool值的比较说明
- 装饰器学习
- java经典排序算法
- Windows 10安装MySQL配置时无响应解决方法
- DHCP服务器回复数据包是单播还是广播的争议
- Selenium2+python自动化7-xpath定位
- 了解资本与公司年报、财报
- C语言之电子时钟程序
- spring事务回滚问题
- 大脑双核单总线模型~《程序员思维的修炼》读后感