zoukankan      html  css  js  c++  java
  • 装饰器2(被装饰函数自带参数)

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    import time
    
    def wrapper(func):
        def deco(*args,**kwargs):#deco接收参数"sunwei"和24
            start = time.time()
            func(*args,**kwargs)#执行此句时,相当于test1("sunwei",24)
            stop = time.time()
            print("the func run %s"%(stop - start))
        return deco
    
    #这里的【@wrapper】等于【test1 = wrapper(test1)】,wrapper(test1),将test1函数以实参的形式传递给wrapper函数,wrapper函数将会把deco函数的内存地址作为返回值返回给变量test1,而此时test1变量所指引的值相当于是deco函数的内存地址,如果test1()则将会执行deco函数
    @wrapper  
    def test1(name,age):
        time.sleep(3)    
        print("in the test1...")
        print("---->%s:%s"%(name,age))
    
    #下面语句相当于deco("sunwei",24)
    test1("sunwei",24)
    在你说话之前,先听;在你回应之前,先想;在你消费之前,先挣;在你退出之前,先试
  • 相关阅读:
    Ethical Hacking
    Ethical Hacking
    Python Ethical Hacking
    Python Ethical Hacking
    Python Ethical Hacking
    食物链 POJ
    Wireless Network POJ
    Candies POJ
    畅通工程再续 HDU
    Jungle Roads HDU
  • 原文地址:https://www.cnblogs.com/sunweigogogo/p/7617062.html
Copyright © 2011-2022 走看看