Python Decorator Initial Solution

Source: Internet
Author: User

Contact Python also has a large half of the time, also carried out a lot of Python related development work, but the understanding of Python is still not deep enough, the current knowledge point is only limited to the basic knowledge commonly used, for more in-depth content are not seriously studied and studied, Recently there is a task to add the log function in the original development process, mastering the adorner technology is very helpful to accomplish this task.

Because before contacting Python is mainly in the knowledge of the front end, for JavaScript knowledge of more, after the knowledge of the adorner before the search, know that python in the adorner and the closure of JavaScript is similar to the wonderful, is to include another function in one function, but there is a big difference in the use of these two parts.

The basic syntax format for adorners is:

Def deco ():    def wrapper ():        print ' AA '    return wrapper@decodef myfunc ():    print ' BB ' MyFunc ()        

In the above code, for the use of the adorner directly using its syntactic sugar form @, so the benefits of writing is obvious, in the subsequent invocation of the time without repeating the function name, but only with the @ instead. A common perception in Python is that everything is objects, so functions are objects. Here the @ symbol can be understood as Myfunc=deco (MyFunc), MyFunc () shorthand.

The advantage of using adorners is that they can be decorated directly with the @ symbol when the decoration function has been written, so as to add new functions without changing the original code.

Python Decorator Initial Solution

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.