html tool

2019年4月10日星期三

python装饰器的递归使用内部方法



参考: http://www.runoob.com/w3cnote/python-func-decorators.html


问题: 想做的被装饰器是否的方法本身递归,而不用带外部装饰器

解决:
  1 def a_perfor_test(perfor_tool, count=3):
  2     print 'count in a_perfor_test::',count
  3     def wrapTheFunction(count):
  4         print("start...doing")
  5         perfor_tool(perfor_tool, count)
  6         print("end...doing")
  7     return wrapTheFunction
  8  
  9 @a_perfor_test
 10 def t(a, count=3):
 11     print "t...%d" % count
 12     if  0 != count:
 13         count = count - 1
 14         a(a, count)
 15     else:
 16         print "none"
 17  
 24 t(5)
测试效果~       

       
# python doing_decorators.py 
count in a_perfor_test:: 3
start...doing
t...5
t...4
t...3
t...2
t...1
t...0
none
end...doing

没有评论:

发表评论