码迷,mamicode.com
首页 > 其他好文 > 详细

通用装饰器

时间:2020-05-17 10:37:25      阅读:60      评论:0      收藏:0      [点我收藏+]

标签:war   sum   ret   fun   color   int   变量   import   函数   

 1 import time
 2 def funcOut(func):
 3     def funcIn(*args,**kwargs):
 4         writeLog(func)
 5         return func(*args,**kwargs)
 6     return funcIn
 7 def writeLog(func):
 8     print(访问方法名:,func.__name__,\t时间:,time.asctime())
 9 @funcOut # 相当于执行:sum = funcOut(sum),
10          # 变量sum 和 funcIn 均指向内部函数,调用内部函数sum(10,20)
11 def sum(a,b):
12     return a + b
13 @funcOut
14 def add(a,b,c):
15     return a + b + c
16 result = sum(10,20)
17 print(两数之和:,result)
18 result = add(10,20,30)
19 print(三数之和:,result)
1 问方法名: sum        时间: Sun May 17 10:24:01 2020
2 两数之和: 30
3 访问方法名: add        时间: Sun May 17 10:24:01 2020
4 三数之和: 60

 

通用装饰器

标签:war   sum   ret   fun   color   int   变量   import   函数   

原文地址:https://www.cnblogs.com/monsterhy123/p/12904115.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!