python decorator

Decorator:

In short, it is to modify a device, which is called a modified object.

This decorated object can be a function, a class...

That is to say, this decorator supplements the function or class without changing the original function/class.

Using a nested function, write the function you modified in one layer of the nested function, and put the function to be modified inside. Finally return the function you defined (remember not to add parentheses, just return the memory address)

Call this decorated method if needed. Then just use @function # in the first line of the function you want to decorate, where the function is the function name of the nested function.

The following case:

 1 #!usr/bin/env python
 2 #encding:utf-8
 3 #by i3ekr
 4 
 5 import time
 6 def pro(func):
 7     def de():
 8         start_time = time.time()
 9         func()
10         end_time = time.time()
11         print("the fun run thime is %s"%(end_time-start_time))
12     return de
13 
14 @pro
15 def f1():
16     time.sleep(3)
17     print "this is f1..."
18 
19 f1()

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324649009&siteId=291194637