Decorator in Python

Source: Internet
Author: User
This article mainly introduces the decorator in Python, which is the basic knowledge in getting started with Python. If you need it, you can refer to the Python decorator to modify the context of the function during code running, that is, you can define the operations performed before the function is executed and after the function is executed, and the function itself has not changed.


First, we define a function, which can output my personal nickname:

def my_name():  print "Yi_Zhi_Yu"my_name() # Yi_Zhi_Yu

So if I need to output my personal uid before the output of my personal nickname, of course, the requirement is that the existing my_name function is not changed, then I can use the decorator at this time.

First, the decorator is also a function. Second, it needs to accept a parameter, which indicates the function to be decorated (my_name ):

def my_info(func):  def wrapper(*args, **params):    print 218    return func(*args, **params)  return wrapper

Then, the method associated with the corresponding decorated function is to use @ my_info to write it before the decorated function.

@my_infodef my_name():  print "Yi_Zhi_Yu"

Finally, when executing my_name, I can output both my uid and my nickname.

my_name()#218#Yi_Zhi_Yu

In the above section, the wrapper function in the decorator function definition is the most confusing one. The decorator itself returns the wrapper function definition, while the wrapper defines the decorator function (my_name) func indicates the function to be decorated. To put it bluntly, the modifier only places a function (a) that cannot be modified into another function (B) and calls a in B, you can do the so-called decoration work before and after the call.
The wrapper function returned by my_info is not the execution result. Only when wrapper is actually executed will the my_name method be actually executed. This is what is said in the closure.
The parameter in wrapper is actually the parameter passed to func (actually my_name ).

Because the decorator is also a function, can the decorator itself transmit parameters. Yes, but you need to define a higher-order function, that is, you need to set a layer of function outside. For example, if I want to output a custom information, you need to pass the Parameter

def c_info(text):  def my_info(func):    def wrapper(*args, **params):      print text      print 218      return func(*args, **params)    return wrapper  return my_info

# Use the decorator @ c_info ("Tony") def my_name (): print "Yi_Zhi_Yu" my_name () # Tony #218 # Yi_Zhi_Yu


Compared with the front decorator, only an outer layer is added, and the inner layer only adds a call to the input parameter (text) of the external layer.

All in all, Python supports the implementation of the decorator in the oop idea in the function definition. In essence, it only uses the closure idea and delays the call, add other implementation content before and after the call.

Ps: The above are all study notes, with their own understanding, it is inevitable that there are deviations, if any leakage found, still hope to correct

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.