#!/usr/bin/env python#--*--coding:utf-8--*--__author__ = ' Kevin ' def deco (func): def In_deco (xx,yy): print (' In Deco ') func (xx,yy) print (' Call deco ') return in_deco# when the Python interpreter sees @deco, it calls the Deco function # incoming parameter bar, which is Deco ( BAR) and returns a function object In_deco#bar = In_deco#bar () In_deco () bar () #实质也是对闭包的使用 @decodef Bar (x, y): print (' In bar ', x+y) print (Type (bar)) bar (ON)
#!/usr/bin/env python#--*--coding:utf-8--*--__author__ = ' Kevin ' def Dec (func): print ' Call dec! ' Def in_dec (*arg): print (' in Dec arg= ', arg) if Len (arg) = = 0: return 0 for Val in a Rg:if not isinstance (val,int): Return 0 return func (*arg) print (' Return in_dec ') return in_dec# When the Python interpreter sees @dec, it will pass #my_sum as a parameter into the DEC # decorated function my_sum point to the new function in_dec# the essence of the adorner is the use of our closures @decdef my_sum (*arg): #my _sum = In_dec print (' in My_sum ') return sum (ARG) #my_sum = Dec (my_sum) Print (My_sum (1,2,3,4,5)) def My_ Average (*arg): return sum (ARG)/len (ARG)
Some things about decorator