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