Adorner is essentially a python function, it can let other functions without any code changes in the premise of adding additional functionality, the following this article mainly introduces you about the use of the adorner in Python, the need for friends can refer to, learn from the following together.
Objective
Recently learning Python, learned why to use the adorner, but also understand what the adorner is, but you may ask, whether you can add a layer of adorners in front of the adorner, what will happen? Just like a building, stacked on top of one another. Actually, it's possible. Now we are going to learn this stacking technique, which is similar to the inheritance of classes and can be inherited continuously. The following words do not say much, come together to see the detailed introduction.
The code is as follows:
#python 3.6 def star (func): def inner (*args, **kwargs): print ("*" *) func (*args, **kwargs) print ("* "*) return inner def percent (func): def inner (*args, **kwargs): print ("% "*) func (*args, **kwargs) print ("%" *) return inner @star @percent def printer (msg): print (msg) printer ("Hello ")
The resulting output is as follows:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%hello%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%**************** **************
In this example, the first output of the asterisk, that is first called the adorner star, then called the second layer of the adorner percent, and finally called the function printer.
Summarize