装饰器加参数


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
  1. 我们重新定义一个test2函数,并传入一个参数name,而函数运行报错 deco()没有传入参数。
  2. @timer 其实相当于 test2 = timer(test2) = deco
  3. 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的更多相关文章

  1. JAVA装饰器模式

    Java程序员们应该对java.io对不会陌生,因为java.io包采用了装饰器模式. 一.定义: Decorator装饰器,顾名思义,就是动态地给一个对象添加一些额外的职责,就好比为房子进行装修一样 ...

  2. python装饰器小计

    1.装饰器:本质是函数,是用来给其他函数添加附加扩展功能的函数,其返回值是一个函数(函数指针) 2.装饰器作用:不改变函数源代码和函数调用方式的前提下添加函数的附加功能. 3.装饰器储备知识点: A. ...

  3. python装饰器同时支持有参数和无参数的练习题

    ''' 预备知识: …… @decorator def f(*args,**kwargs): pass # 此处@decorator  等价于 f = decorator(f) @decorator2 ...

  4. python装饰器1:函数装饰器详解

    装饰器1:函数装饰器 装饰器2:类装饰器 装饰器3:进阶 先混个眼熟 谁可以作为装饰器(可以将谁编写成装饰器): 函数 方法 实现了__call__的可调用类 装饰器可以去装饰谁(谁可以被装饰): 函 ...

  5. python05 - 迭代器,生成器,装饰器

    迭代器 迭代器就是访问集合元素的一种方式,迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问一遍后结束. 迭代器很大的特点: 只能往前迭代,不能够回退,也不能随机访问其中一个元素,只能通过__ ...

  6. python基础整理4——面向对象装饰器惰性器及高级模块

    面向对象编程 面向过程:根据业务逻辑从上到下写代码 面向对象:将数据与函数绑定到一起,进行封装,这样能够更快速的开发程序,减少了重复代码的重写过程 面向对象编程(Object Oriented Pro ...

  7. Javascript装饰器的妙用

    最近新开了一个Node项目,采用TypeScript来开发,在数据库及路由管理方面用了不少的装饰器,发觉这的确是一个好东西.装饰器是一个还处于草案中的特性,目前木有直接支持该语法的环境,但是可以通过 ...

  8. python装饰器执行顺序

    . python 装饰器 1) 2层装饰器 def decorator(func): # TODO def wrapper(*args, **kwargs): # TODO func(*args, * ...

  9. Python学习系列之装饰器

    装饰器的作用 装饰器用于装饰某个函数.方法或者类,它可以让这个函数执行之前或者执行之后做一些操作 手工实现一个装饰器 def outer(some_func): #装饰器 $1 def inner() ...

随机推荐

  1. jpa的@Query中"?"占位符的使用小坑

    今天使用@Query自定义查询语句,出现了一个错误: java.lang.IllegalArgumentException: Parameter with that position [1] did ...

  2. 【Pthon入门学习】99乘法表

    学习知识点: 1. string.join(seq): 以string字符串作为分隔符,将seq的所有元素拼接成一个新的字符串 s = ['%d * %d = %d' % (y, 4, 4*y) fo ...

  3. C语言零碎知识点

    1.  int整形在64位和32位计算机中都占4个字节. 指针在64位占8个字节,32位占4个字节. 2.  数组下标从0开始,a[0]开始,链表下标从1开始,a[1]开始. 3. 条件运算符(con ...

  4. eclipse启动一闪而退

    eclipse启动一闪而退 打开eclipse,启动画面一闪而过退出. 解决方法: 以下每一步结束都重启eclipse一下,看能不能正常启动. 1. 在C:/WINDOWS/system32 系统文件 ...

  5. pyextend库-unpack列表集合字符串解包函数

    pyextend - python extend lib unpack (iterable, count, fill=None) 参数: iterable: 实现 __iter__的可迭代对象, 如 ...

  6. C语言文法翻译

    <程序>→<外部声明>|<程序><外部声明> <外部声明>→<函数定义>|<声明> <函数定义>→< ...

  7. 读着读着《构建之法》(Build To Win) 越精神的白雪儿的思考

    哲学家的宗旨是:我思,故我在 科学家的宗旨是:我发现,故我在 工程师的宗旨是:我构建,故我在 ——<工程学--无尽的前沿> 序言:珍惜角色“人”,注重实践“物” <构建之法>, ...

  8. CentOS7 修改分辨率

    1. 修改文件: vi /boot/grub2/grub.cfg 2. 在linux16 开头的哪一行 增加 vga=0x341 修改为1024x768 3. 重启..

  9. Linux服务器启动后只读解决办法

    今天处理一个服务器,远程死活连接不上,只好跑信息中心去看了下服务器. Linux服务器启动之后,提示: give root password for maintenance (or type cont ...

  10. Halcon 笔记2 Blob分析

    1. 数组操作 2. 可视化-更新窗口 (1)单步模式-总是:则可以自动显示图像: (2)单步模式-从不:需要调用显示函数才能显示图像. (3)单步模式-清空显示:将原图清除,再显示新图 3. 图像处 ...