装饰器--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() ...
随机推荐
- Paper Reading - Convolutional Sequence to Sequence Learning ( CoRR 2017 ) ★
Link of the Paper: https://arxiv.org/abs/1705.03122 Motivation: Compared to recurrent layers, convol ...
- python sys.argv是什么?
1.sys.argv 是获取运行python文件的时候命令行参数,且以list形式存储参数 2.sys.argv[0] 代表当前module的名字 下面的代码文件是a.py,当我不用IDE工具,只用命 ...
- Python学习之web框架 Flask
一.通过PIP 安装Flask 1.1 Windows环境安装pip A.首先PIP进入官网(https://pypi.python.org/pypi/pip)下载gz包 B.对gz压缩包进行解压,解 ...
- hadoop的safemode 安全模式
hadoop启动检查副本块数,就会进入safemode safemode的相关情况 虽然不能进行修改文件的操作,但是可以浏览目录结构.查看文件内容的. 在命令行下是可以控制安全模式的进入.退出和查看的 ...
- Python3 迭代器和生成器
想要搞明白什么是迭代器,首先要了解几个名词:容器(container).迭代(iteration).可迭代对象(iterable).迭代器(iterator).生成器(generator). 看图是不 ...
- 王者荣耀交流协会第一次scrum会议
照片: 拍照的人是我(高远博),没有出镜.开会时间是17:00到17:37. 昨天的成绩: (1)优化了折线图界面 今天的计划: (1)小组成员汇报昨日成果. (2)小组成员继续推进任务. 遇到的困难 ...
- jQuery之过滤元素
还是那句话,这些知识一个小小的练习,更多的请看jQuery手册 在jQuery对象中的元素对象数组中过滤出一部分元素来1. first()2. last()3. eq(index|-index)4. ...
- Destoon 模板存放规则 及 语法参考
模板存放规则及语法参考 一.模板存放及调用规则 模板存放于系统 template 目录,template 目录下的一个目录例如 template/default/ 即为一套模板 模板文件以 .htm ...
- django的第一个问题
/usr/local/lib/python2.7/dist-packages/allauth/account/utils.py in setup_user_email, line 258 /usr/l ...
- js中关于array的常用方法
最近总结了一些关于array中的常用方法, 其中大部分的方法来自于<JavaScript框架设计>这本书, 如果有更好的方法,或者有关于string的别的常用的方法,希望大家不吝赐教. 第 ...