Talking about the exploration of python decorators and the collection of parameters, talking about the exploration of python decorations

Source: Internet
Author: User

Talking about the exploration of python decorators and the collection of parameters, talking about the exploration of python decorations

First, go to the original article:

Now, let's assume that we want to enhance the functions of the now () function, for example, to automatically print logs before and after the function call, but do not want to modify the definition of the now () function, this way of dynamically adding features during code execution is called the Decorator ).

In essence, decorator is a high-level function that returns a function.

What is the essence of Decorator?

I tried it ..

Def g (): print ("here is G") return "G" @ gdef f (): print ("here is F") return 1 ''' -------------------------------------------------- line 5, in <module> @ gTypeError: g () takes 0 positional arguments but 1 was given >>>> '''

Run the result in the comment

Embarrassed... g is forced to insert a parameter, which should be g "modified object"

Modify and continue .....

Def g (f): print ("here is G") return "G" @ gdef f (): print ("here is F ") return 1 ''' ---------------------------------------------- here is G> f () Traceback (most recent call last): File "<pyshell #0>", line 1, in <module> f () TypeError: 'str' object is not callable '''

The str object cannot be called. Here, there is only one str, which is the return value of g.

To verify, I changed "G" to 2

The result is

TypeError: 'int' object is not callable

OK, which means that the decorator is first inserted with a parameter, and then the return value is called again. However, it seems that only the function can be called. Therefore, in order not to report an error, the decorator must return a function. The decorator must be a high-order function ......

I mean I'm not satisfied. Isn't it a function? The g parameter is a function.

Def g (f): print ("here is G") return f @ gdef f (): print ("here is F ") return 1 ''' ------------------------------------------------ here is G >>> f () Here is f'1 ''''

It runs successfully. But... say "log printing", # yes. "Here is G" is the log I want.

Question 1:The printed "G" is the first line, which occurs before the input "f ....

Question 2:After entering "f ()", "Here is G" is not displayed .....

# Simply look at the g function, it is not a "high-order function"

As an example of success, it is too failed.

# Well, I surrender, and it's not very interesting to be stubborn .....

The closure tells us the truth. To ensure that the returned value must be a function, the best way is to "Create a function inside the function and then throw it out"

Def g (f): print ("here is G") def h (): print ('here is H') return "H" return H @ gdef f (): print ("here is F") return 1 ''' ------------------------------------------------ here is G >>> f () Here is h'h' >>> f () here is H 'H'> '''

The f function is not executed. Yes, yes. I tried it again.

In addition, the two logs can only be used... (as described below)

After reading the book, if the h function returns f (), the f function will be executed"

Def g (f): print ("here is G") def h (): print ('here is H') return f () return H @ gdef f (): print ("here is F") return 1 ''' ---------------------------------------------- here is G >>> f () Here is H here is f'1' >>> F <function g. <locals>. h at 0x0000020CBDBB6C80> '''

According to the ideas in the book

''' @ Gdef f (): pass >>> f () is equivalent to >>> g (f) () g Function execution, return >>> h () h function execution (log printing) >>> f () f execution, return 1 >>> 1 '''

Add parameters,

Def g (f): print ("here is G") def h (* args, ** kw): print ('here is H') return f (* args, ** kw) return h @ gdef f (* args, ** kw): print ("F ") return "1" ''' >>> f (* args, ** kw) is equivalent to >>> g (f) (* args, ** kw) g Function execution, return >>> h (* args, ** kw) h function execution (log printing) >>> f (* args, ** kw) f execution, returns 1> 1 '''

It can be seen that (* args, ** kw) is taken away by the h function. So, observe the h function, and h gives its parameters to f.

I am confused.

Def g (f): print ("here is G") def h (): # The parameter print is not required for h ('here is H ') return f return h @ gdef f (* args, ** kw): print ("F") return "1" ''' >>>> f () (* args, ** kw) is equivalent to> g (f) (* args, ** kw) g Function execution, return> h () (* args, ** kw) h function execution, null parameter in h (print log) >>> f (* args, ** kw) f execution, returns 1> 1 '''

However, a new problem arises. An empty bracket is added when the fbutton is used later. Otherwise

''' >>>> F (* args, ** kw) is equivalent to >>> g (f) (* args, ** kw) g Function execution, return >>> h (* args, ** kw) h function execution (log printing) returns f >>> f is a function object '''

The above tells us the truth: "No execution of a function depends on whether there are any parentheses"

For example

def m(a): print(a) return mprint(m(1)(2)(3)(4)(5)(6)(7)(8)(9)(10))'''-----------------------------246810<function m at 0x000002832BDB10D0>

Brain burning time

F = a. B. c () [0] () [d () [e]

The Class B c method of module a is a high-order function, and a list is returned. The list contains a function.

The function returns a dictionary ............

The above discussion about the exploration of python decorators and the collection of parameters is all the content shared by the editor. I hope to give you a reference and support for the house of helpers.

Related Article

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.