装饰器加参数


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. scrum立会报告+燃尽图(第三周第二次)

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2284 项目地址:https://coding.net/u/wuyy694 ...

  2. 团队作业——王者光耀:team

    光耀101  <光耀101>是福州大学数计学院计算机专业推出的中国首部程序猿脱发养成节目.由张栋担任发起人,刘晨瑶.畅畅担任导师.  该节目召集了你猜多少位选手,通过任务.训练.考核,让选 ...

  3. DP----入门的一些题目(POJ1088 POJ1163 POJ1050)

    动态规划入门 DP 基本思想 具体实现 经典题目 POJ1088 POJ1163 POJ1050 (一) POJ1088,动态规划的入门级题目.嘿嘿,连题目描述都是难得一见的中文. 题目分析: 求最长 ...

  4. 本周WEB技术学习情况

    相较于之前本周有了明显的进步    之前不熟悉的知识点在多次的实机中得到巩固加深,用得越来越得心应手 我认为只要多用功更加自主的学习  web其实并不难 希望自己天天进步

  5. C++变量内存分配及类型修饰符

    前言 了解C++程序内存分配,有助于深刻理解变量的初始化值以及其生存周期.另外,变量类型修饰符也会影响到变量的初始化值及其生存周期.掌握了不同类型变量的初始化值及其生存周期,能够让我们设计程序时定义变 ...

  6. Struts2文件的上传和下载实现

    <一>简述: Struts2的文件上传其实也是通过拦截器来实现的,只是该拦截器定义为默认拦截器了,所以不用自己去手工配置,<interceptor name="fileUp ...

  7. C语言为运算及 两个变量的赋值问题

    #include <stdio.h>#define ARRAY_SIZE 10int main() {    int arr[ARRAY_SIZE] = {51,116,53,120,85 ...

  8. Spring Boot 学习资料【m了以后看】(转)

    推荐博客: 程序员DD SpringBoot集成 liaokailin的专栏 纯洁的微笑 SpringBoot揭秘与实战 catoop的专栏 方志朋Spring Boot 专栏 简书Spring Bo ...

  9. 【vim】vim常用命令

    移动: h 或 向左箭头键(←)  #光标向左移劢一个字符 j 或 下箭头键(↓)    #光标向下移劢一个字符 k 或 向上箭头键(↑)    #光标向上移劢一个字符 l 或 向右箭头键(→)    ...

  10. H5跳转到百度地图并定位

    找了半天的JS api,发现没有,后来发现这个叫 url api,让我好找. 官方文档: http://lbsyun.baidu.com/index.php?title=uri/api/web : 简 ...