Python basic article "Nineth": Anatomy Decorator

Source: Internet
Author: User

Adorner:
Adorner: The adorner's syntax begins with @, followed by the adorner function's name and optional parameters, followed by an optional parameter that the adorner declares to be a decorated function and a decorative function.
Anatomy Decorator:
#!/usr/bin/env Python3 ' Adorner: The adorner's syntax begins with @, followed by the adorner function's name and optional parameters, followed by the adorner's declared optional parameters for the modified function and the adornment function. Expression: Def outer (): def Inne  R (): Print ("XXX") return Test return inner @outer def F1 (): Pass F1 () "# Def Outer (func): # def inner (*args, **kwargs): # Print ("123") # ret = func (*args, **kwargs) # #print ( RET) # print ("456") # return ret# return innerdef outer_0 (func): Def inner (*args, **kwargs): P Rint ("3.5") ret = func (*args, **kwargs) print (ret) return ret return inner@outer_0# @outerdef Inde X (A1,A2): Print ("This is the content of the INDEX function! ") return a1 + a2# ret = index (+) # print (ret) index # @outer_0 # def F1 (): # Print (" F1 ") # return" AAAAA "# F1    () "Above adorners performed by anatomy: 1. The program starts executing, sees the function" Def outer (func) ", does not execute, puts this function into memory, and then continues to execute.    2. See the function "Def outer_0 (func):" Is put into memory and will not be executed.      3. When you go down to see @outer_0 and execute this function, the interpreter will pass the function "index" below @outer_0 as an argument to the parameter "func" in the "outer_0" function, so now "index" = "Func"  def outer_0 (index): #拿到index函数, pass it as an argument into the function def inner (1, 2): #                        The second-level function (which will find the corresponding value from the primary function, which will pass in the index parameter), the body parameter corresponds to the entity parameter so *args = 1, # #kwargs = 2 print ("3.5") #自己函数体的值 print ("This is the content of the INDEX function! ") #从index函数中拿到的值 ret = func (1, 2) #在这赋值一个变量ret, however the value of this variable is func (1, 2), why?                Because Func is the return value of index 1, and 2 is the entity parameter Print (RET) from index #然后把这个返回值给输出出来 return ret #最后把返回值给上一层的函数 return inner 4. Then redefine the return value of Outer_0 to the "index" function, that is, "index" equals "O Uter_0 "return value! This step is equivalent to recreating a new" index "Function! and the content of this new" index "function is the return value of" Outer_0 "! ("Index" = return value of "Outer_0")

Code Execution Results:

C:\Users\Administrator\AppData\Local\Programs\Python\Python35\python.exe d:/pycharm/obj/old_boy/day07/ Adorner. py3.5 this is the content of the INDEX function! 3Process finished with exit code 0

* Note: Multiple adorners are handled when multiple decorators are executed.

@outer @outer_0def Index ():    print ("Test") Index () #等于一下方式执行:    (outer*index) *outer_0    (OUTER*OUTER_0) * Index is calculated as a multiplication method!

  

  

Python basic article "Nineth": Anatomy Decorator

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.