[Python] understand the adorner

Source: Internet
Author: User
Tags python decorator

In OOP it is necessary to implement through inheritance, and Python, in addition to using inheritance to implement the adornment mode, directly below the level of the syntax to support the adorner through a simple example to understand the Python decorator here I will design a log class, when writing code may encounter such a problem, Need to record all the operations, how can this requirement be implemented?
#python decoratordeflog (text):defDecorator (func):defWapper (*args):Print('%s%s:'% (Text,func.__name__))            returnFunc (*args)returnWapperreturnDecorator

To analyze this code carefully, the log () function takes a text message, returns a decorator function, and then the function is to receive a new function and print some information before calling the function;
@log (' get time ')defprint('2017-4-15 11:11:11'= gettime () Now ()

Results

[Python] understand the adorner

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.