装饰器--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() ...
随机推荐
- mysql查看锁表与解锁
查看锁表语句 show OPEN TABLES where In_use > 0; 找到锁表的进程 show processlist; 删除锁表进程 kill 51045123;
- USACO 2.3.4 Money Systems 货币系统(完全背包)
Description 母牛们不但创建了他们自己的政府而且选择了建立了自己的货币系统. [In their own rebellious way],,他们对货币的数值感到好奇. 传统地,一个货币系统是 ...
- 四则运算2+psp0
程序要求: 1.题目避免重复 2.可定制(数量\打印方式) 3.可以一下控制参数 ① 是否有乘除法 ② 是否有括号(最多支持十个数参与运算) ③ 数值范围 ④加减有无负数 ⑤除法有无余数 分析:① 如 ...
- Python:模块学习——os模块
os模块提供了多个访问操作系统服务的功能 os模块中一些重要的函数和变量 os.name 显示当前使用平台 os.getcwd() 显示当前Python脚本工作路径 os.listdir('dirna ...
- Improving the Safety, Scalability, and Efficiency of Network Function State Transfers
Improving the Safety, Scalability, and Efficiency of Network Function State Transfers 来源:ACM SIGCOMM ...
- Visual C++ 8.0对象布局
哈哈,从M$ Visual C++ Team的Andy Rich那里又偷学到一招:VC8的隐含编译项/d1reportSingleClassLayout和/d1reportAllClassLayout ...
- bond下改变网卡
浪潮服务器打开控制台 用ip addr查看哪个网卡是绑定的,eth2和eth4是绑定状态 用mv命令,更改网卡名称 并将每个网卡里的信息更改 reboot,重启 ip addr查看,eth6和eth8 ...
- oracle impdp导入时 提示“ORA-39002: 操作无效 ORA-39070: 无法打开日志文件 ”
第一步:首先使用DBA权限的用户创建directory,我使用system ,可以在服务器本地创建,也可以远程连接sqlplus进行创建,使用的将是服务器上面的路径.要确保创建directory时,操 ...
- Kafka设计解析
Kafka剖析(一):Kafka背景及架构介绍 Kafka设计解析(二):Kafka High Availability (上) Kafka设计解析(三):Kafka High Availabilit ...
- Jquery简单实现Datepicker
cshtml: <input type="text" id="purchaseDate" name="PurchaseDate" va ...