yzqzs 发表于 2018-8-15 14:07:52

python之装饰器 实例

=====================================写法1==========================  
import time
  
def timer(func):
  
    def deco():
  
      start_time = time.time()
  
      func()
  
      stop_time = time.time()
  
      print('the func run time is %s' %(stop_time - start_time
  
    return deco
  
def test1():
  
    print('this is test1')
  
def test2():
  
    print('this is test2')
  
test1 = timer(test1)把deco内在地址赋值给test1
  
test1()#test1未被改变,而且调用方式也未被改变
页: [1]
查看完整版本: python之装饰器 实例