In the past, most programming languages were too simple to learn. basically, similar content such as decorators was rarely involved. I always think it is a magical thing and I am reluctant to learn it ). I read a book today. The principle of discovery is quite simple. The main function of the decorator is to modify functions. it appears to define static methods when introducing class and static methods. For example, to declare the foo () function as a static function
The code is as follows:
Class Myclass (object ):
Def staticfoo ():
............
............
Staticfoo = staticmethod (staticfoo)
You can use the decorator method to achieve this:
The code is as follows:
Class Myclass (object ):
@ Staticmethod
Def staticfoo ():
.........
.........
This example is obviously easy to understand.
Let's take the following example. This example also involves an important part, that is, the essential understanding of functions in python.
Code:
The code is as follows:
#-*-Coding: UTF-8 -*-
From time import ctime
From time import sleep
Def ftfunc (func ):
Def timef ():
Print "[% s] % s () called" % (ctime (), func. _ name __)
Return func ()
Return timef
@ Ftfunc
Def foo ():
Print 'Hello'
If _ name _ = '_ main __':
Foo ()
Sleep (2)
For I in range (2 ):
Sleep (1)
Foo ()
Run this code. we can see that the terminal will output the following content in sequence:
Here, the ftfunc function is a self-defined function. This function is a function as a parameter, which meets the requirements of being a decorator, according to the above equivalent conversion rule for the decorator, this code
The code is as follows:
@ Ftfunc
Def foo ():
Print 'Hello'
It can be converted into the following code:
The code is as follows:
Def foo ():
Print 'Hello'
Foo = ftfunc (foo)
Combined with the original code above, we will soon be able to experience the role of the decorator.
However, when I was writing this code, an error occurred:
This code:
The code is as follows:
Return func ()
Return timef
I wrote:
The code is as follows:
Return func
Return timef
Therefore, the output results are different. later, an important concept was found: "foo" is a reference to a function object, while "foo ()" is a call to a function object. Object reference is an important basic concept of python. in python, everything is an object, and the type is an object, not a variable. All variables are just object references, which is equivalent to directing this variable to this object. "Foo" can be understood as a variable, but it points to the object of a function. "Foo ()" is a function object call. to call this object, you must execute the function of this function. Here, you need to understand your taste. Based on this:
The running result of such a piece of code is exactly the same as that of the previous one. Note that comparing the differences with the code just now makes it easier to understand.
The code is as follows:
#-*-Coding: UTF-8 -*-
From time import ctime
From time import sleep
Def ftfunc (func ):
Def timef ():
Print "[% s] % s () called" % (ctime (), func. _ name __)
Return func
Return timef
@ Ftfunc
Def foo ():
Print 'Hello'
If _ name _ = '_ main __':
Foo ()()
Sleep (2)
For I in range (2 ):
Sleep (1)
Foo ()()
Running result of this code:
In fact, you can also add parentheses to the returned timef function to see what the results will look like. You can better understand the concept of functions in python.