Examples of adorner-level connectors in Python

Source: Internet
Author: User
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

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.