[Python Advanced]002. Decorator (1)

Source: Internet
Author: User
Tags base64 python decorator

Decorators (1)

    • Introduced
    • HelloWorld
      • Demand
      • Using functional programming
      • Add decorator
      • Analytical
Introduced
    • The python decorator is called Decorator , which is to decorate a module.
    • Action: Adds additional functionality to an existing object.
    • Similar to annotations in Java, this method is decorated with @xxx in front of the method.
    • Unlike annotations in Java, Python's adorners are fairly straightforward.
    • Function-Type programming
    • Slicing-oriented programming
HelloWorld demand
def Fun (i):     Print I

This is a simple method, and now we are going to execute some other code before and after executing this method, such as calculating the run time.
The Simple Way is:

Import  Time def Fun (i):     = time.time ()    print  i    = Time.time ()-T     #  calculate run time    Print ' Run Time: ', str (t)    #  print Time

This method is inconvenient and cannot be reused.

Using functional programming
Import Time#Calculate run TimedefExec_time (FN, i):#fn: method; I: Parameters of the methodt =time.time () fn (i)#call FNt = time.time ()-T#Calculate run Time    Print 'Run Time:', str (t)#Print TimedefFun (i):PrintI#calledExec_time (fun, i)

In this way, in the actual invocation, it is necessary to wrap a layer exec_time of method, although it can be reused, but it is not flexible.

Add decorator

Now we're going to add modifiers.

 fromFunctoolsImportWraps#introduce the package required by the adornerImport Time#define an adornerdefExec_time (FN):#parameter fn: The method to be modified is our fun@wraps (FN)def_ (i):#modification Method, parameter I is fun to pass over the parameterst =time.time () fn (i)#Execution Methodt = time.time ()-TPrint 'Run Time:', str (t)return_#returns the post-decoration method

This completes an adorner that calculates the run time.

@exec_time def Fun (i):     Print I@exec_time def fun2 (i):     Print ' fun2 ', I

It is very convenient to use.

Analytical
    • @exec_timeafter the method is added fun , the equivalent method is called automatically when the call is made exec_time(fun) .
    • funThe method is exec_time decorated, the method is timed at the beginning and end, and the print takes time, which is a cross section, which is the way of programming 面向切面的编程 . The horizontal insertion of logic during method execution can reduce duplication of code in many places.

This site article is for baby bus SD. Team Original, reproduced must be clearly noted: (the author's official website: Baby bus )
Reprinted from "Baby bus Superdo Team" original link: http://www.cnblogs.com/superdo/p/4829345.html

[Python Advanced]002. Decorator (1)

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.