Python's Decorator

Source: Internet
Author: User

Adorner:

1 Import Time2 defcal (L):3Start_time=time.time ()4res=05      forIinchL:6Time.sleep (0.1)7res+=I8Stop_time =time.time ()9     Print('the run time of the function is%s'% (stop_time-start_time))Ten     returnRes One  A Print(Cal (range (100)))
View Code

Decorator preview:

1 Import Time2 defTimmer (func):3     defWrapper (*args,**Kwargs):4Start_time=time.time ()5Res=func (*args,**Kwargs)6Stop_time =time.time ()7         Print('function run time is%s'% (stop_time-start_time))8         returnRes9     returnwrapperTen @timmer One defcal (L): Ares=0 -      forIinchL: -Time.sleep (0.1) theres+=I -     returnRes -  -Res=cal (Range (10)) + Print(RES)
View Code

Higher order functions:

1. The parameter received by the function is a functional name
2. The return value of a function is a functional name
3. Any one of the above conditions can be called the higher order function

  

Python's Decorator

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.