Python Fundamentals: Decorators and closures

Source: Internet
Author: User
Tags access properties instance method

#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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.