装饰器--decorator2
装饰器加参数
import time def timer(func): # timer(test2) func = test2
def deco():
start_time = time.time()
func() # run test1()
stop_time = time.time()
print("the action time of the program is {}".format(stop_time-start_time))
return deco # 返回了deco的内存地址 @timer # test2 = timer(test2)=deco
def test2(name):
time.sleep(2)
print("the name is {}".format(name)) test2("bigberg") # test2("bigberg") = deco("bigberg") #输出
Traceback (most recent call last):
File "G:/python/untitled/study3/decoration4.py", line 17, in <module>
test2("bigberg")
TypeError: deco() takes 0 positional arguments but 1 was given
- 我们重新定义一个test2函数,并传入一个参数name,而函数运行报错 deco()没有传入参数。
- @timer 其实相当于 test2 = timer(test2) = deco
- test2("bigberg") = deco("bigberg")
所以我们需要在嵌套函数 deco()中传入一个参数,才能确保程序正确
import time def timer(func):
def deco(arg1):
start_time = time.time()
func(arg1)
stop_time = time.time()
print("the action time of the program is {}".format(stop_time-start_time))
return deco # 返回了deco的内存地址 @timer
def test2(name):
time.sleep(2)
print("the name is {}".format(name)) test2("bigberg") #输出
the name is bigberg
the action time of the program is 2.0001132488250732
到这里传参已经实现了,但是如果参数个数不固定呢?我们还有非固定参数:
import time def timer(func): # timer(test1) func = test1
def deco(*args,**kwargs):
start_time = time.time()
func(*args,**kwargs) # run test1()
stop_time = time.time()
print("the action time of the program is {}".format(stop_time-start_time))
return deco # 返回了deco的内存地址 @timer
def test1():
time.sleep(2)
print("in the test1.") @timer
def test2(name,age):
time.sleep(2)
print("the name is {} and age is {}".format(name,age)) test1()
test2("bigberg",18) #输出
in the test1.
the action time of the program is 2.0002853870391846
the name is bigberg and age is 18
the action time of the program is 2.000582218170166
由此可见使用非固定参数后,被修饰函数有没有参数都可以正常运行了。
装饰器--decorator2的更多相关文章
- JAVA装饰器模式
Java程序员们应该对java.io对不会陌生,因为java.io包采用了装饰器模式. 一.定义: Decorator装饰器,顾名思义,就是动态地给一个对象添加一些额外的职责,就好比为房子进行装修一样 ...
- python装饰器小计
1.装饰器:本质是函数,是用来给其他函数添加附加扩展功能的函数,其返回值是一个函数(函数指针) 2.装饰器作用:不改变函数源代码和函数调用方式的前提下添加函数的附加功能. 3.装饰器储备知识点: A. ...
- python装饰器同时支持有参数和无参数的练习题
''' 预备知识: …… @decorator def f(*args,**kwargs): pass # 此处@decorator 等价于 f = decorator(f) @decorator2 ...
- python装饰器1:函数装饰器详解
装饰器1:函数装饰器 装饰器2:类装饰器 装饰器3:进阶 先混个眼熟 谁可以作为装饰器(可以将谁编写成装饰器): 函数 方法 实现了__call__的可调用类 装饰器可以去装饰谁(谁可以被装饰): 函 ...
- python05 - 迭代器,生成器,装饰器
迭代器 迭代器就是访问集合元素的一种方式,迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问一遍后结束. 迭代器很大的特点: 只能往前迭代,不能够回退,也不能随机访问其中一个元素,只能通过__ ...
- python基础整理4——面向对象装饰器惰性器及高级模块
面向对象编程 面向过程:根据业务逻辑从上到下写代码 面向对象:将数据与函数绑定到一起,进行封装,这样能够更快速的开发程序,减少了重复代码的重写过程 面向对象编程(Object Oriented Pro ...
- Javascript装饰器的妙用
最近新开了一个Node项目,采用TypeScript来开发,在数据库及路由管理方面用了不少的装饰器,发觉这的确是一个好东西.装饰器是一个还处于草案中的特性,目前木有直接支持该语法的环境,但是可以通过 ...
- python装饰器执行顺序
. python 装饰器 1) 2层装饰器 def decorator(func): # TODO def wrapper(*args, **kwargs): # TODO func(*args, * ...
- Python学习系列之装饰器
装饰器的作用 装饰器用于装饰某个函数.方法或者类,它可以让这个函数执行之前或者执行之后做一些操作 手工实现一个装饰器 def outer(some_func): #装饰器 $1 def inner() ...
随机推荐
- Is It A Tree?(并查集)
Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a ...
- 王者荣耀交流协会scrum立会20171111
1.立会照片 成员王超,高远博,冉华,王磊,王玉玲,任思佳,袁玥全部到齐. master:高远博 2.时间跨度: 2017年11月10日 18:00 - 18:33 ,总计33分钟. 3.地 点: 一 ...
- Ubuntu环境下No module named '_tkinter'错误的解决
在Ubuntu环境下运行下面代码: import matplotlib as plt 出现以下错误: No module named '_tkinter' 解决方法: sudo apt-get ins ...
- 团队Alpha冲刺(四)
目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:丹丹 组员7:何家伟 组员8:政演 组员9:鸿杰 组员10:刘一好 组员:何宇恒 展示组内最新 ...
- HDU 5195 DZY Loves Topological Sorting 拓扑排序
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5195 bc(中文):http://bestcoder.hdu.edu.cn/contests ...
- Alpha 冲刺9
队名:日不落战队 安琪(队长) 今天完成的任务 协助开发手写涂鸦demo. okhttp学习第三弹. 明天的计划 协助开发语音存储demo. 还剩下的任务 个人信息数据get. 遇到的困难 困难:整理 ...
- 人生的第一篇blog
开始写博客了,人生第一篇博客啊,要写些什么呢?想想也没有什么头绪,随便写写吧. 这学期要使用代码管理工具了,要写团队项目了.一直以来都是自己一个人在默默编程,没有过合作经历.对于代码的管理也只是一直在 ...
- CentOS7安装Consul集群
1.准备4台服务器 linux1 192.168.56.101 linux2 192.168.56.102 linux3 192.168.56.103 linux4 192.168.56.104 2. ...
- erlang访问https地址
参考How do I do an HTTPS request with Erlang? 1> application:start(inets). ok 2> application:sta ...
- 一些有趣的erlang项目
这里会收集一些erlang项目,有需可以转. erlang-bookmarks Scaling Erlang High Performance Erlang - Finding Bottlenecks ...