#4. Decorative Device#Closed PackagedefFX (x): x+ = 1deffy (y):returnx*yreturnFYdefF1 (func):#print (' F1 runing ') defF2 (y):Print('F2 runing') returnFunc (y) + 1returnF2defGun (m):Print('Gun runing') returnm*m#F1 (gun)#FG = F1 (gun)defFun (m):Print('F2 runing') returnGun (m) + 1#FG (1)#Fun (1)@f1defDeco (m):Print('this is Deco .') returnM*m#here, the function of the adorner is achieved by adding 1 to the return value of Deco . """For example, a normal person will wear shoes, but some people in the shoes will be put a high insole, this increase insole does not affect wear shoes, but it will let people look taller, the adorner is like this booster insole, it does not affect the normal use of functions, but can add more functions to the function """Import TimedefRun_time (func):defNew_fun (*args,**Kwargs): T0=time.time ()Print('Star Time:%s'% (Time.strftime ('%x', Time.localtime ())) ) Back= Func (*args)Print('End time:%s'% (Time.strftime ('%x', Time.localtime ())) ) Print('run time:%s'% (round (time.time ()-T0), 4) return BackreturnNew_fun @run_timedefTest (): forIinchRange (1,10): forJinchRange (1,i+1): Print('%dx%d=%2s'% (j,i,i*j), end =' ') Print () classTest_class:def __init__(self,func): Self.func=funcdef __call__(self):Print('class') returnSelf.func @Test_Classdeffun_test ():Print('This is a test function .') #fun_test () () #4. Class DecoratorsclassRectangle:def __init__(self,length,width): Self.width=width self.length=lengthdefArea (self):#instance MethodAreas = Self.length *Self.widthreturnareas @property#access methods just like access properties defFun (self):returnself.width*self.length @staticmethod#don't preach self. deffunc ():Print('Staticmethod func') @classmethoddefShow (CLS):Print(CLS)Print('Show Fun') e= Rectangle (3,4) F= Rectangle (6,8)
Python Fundamentals: Decorators and closures