Summary:
Decorator (Decorators) is an important part of Python. Simply put: they are modified functional capabilities of other functions.
For example, we write flask, the route is to use a decorator defined. If write access control, the access control is generally realized by a decorator. Logging, generally may be implemented by decorators.
Simply put, it is to increase the practice of certain functions to one or several functions.
The following example implementation.
A: basic functions
1. Source
from time import sleep def watch_movie(): print('看电影') sleep(3) print('The End') if __name__ == '__main__': watch_movie()
2. Perform results
The code is simple, first print cinema, an interval of 3 seconds to print The End.
Two: decorator principle
1. Goal: compute a function of time
2. Source
from time import sleep, time def ceal_time(): before = time() watch_movie() after = time() print('函数运行%s秒' % (after - before)) def watch_movie(): print('看电影') sleep(3) print('The End') if __name__ == '__main__': ceal_time()
3. Perform results
The code is simple, first print cinema, an interval of 3 seconds to print The End, then print run timing function.
4. Analysis
We put a function into another function to run, which is the basic principle of the decorator.
Three: the transformation of timing functions as a generic function
1. Objective: to calculate a function of time this function, adapted to different functions.
2. Source
from Time Import SLEEP, Time DEF ceal_time (Fun): before = Time () Fun () After = Time () Print ( ' function running% s s ' % (After - before)) DEF watch_movie (): Print ( ' see film ' ) SLEEP ( 3 ) Print ( ' at The End ' ) DEF play_game (): Print ( ' playing games ' ) SLEEP (3) print('Game Over') if __name__ == '__main__': ceal_time(watch_movie) ceal_time(play_game)
3. Perform results
Watching movies and playing games two functions are carried out.
4. Analysis
We can function as an object, which passed in another function.
Four: become a decorator
1. Objectives:
We changed the function is called, can not fail to change the function code in the position to call it?
2. Source:
from time import sleep, time def ceal_time(fun): def wrapper(): before = time() fun() after = time() print('函数运行%s秒' % (after - before)) return wrapper @ceal_time def watch_movie(): print('看电影') sleep(3) print('The End') # @ceal_time def play_game(): print('玩游戏') sleep(3) print('Game Over') if __name__ == '__main__': watch_movie() play_game()
3. Perform results
Look in front of the movie plus a decorator to achieve the function run time, play games without adding decorator, so there is no timing function runs.
And in the main function is called and did not increase in the same decorators.
Five: Functions have arguments
1. Objectives: function to be decorated, with a processing parameter
2. Source:
from Time Import SLEEP, Time DEF ceal_time (Fun): DEF warpper (* args, ** kwargs): # modified before = Time () Fun ( * args, ** kwargs) # modifications After = Time () Print ( ' function second running% s ' % (After - before)) return warpper @ceal_time DEF watch_movie (name, movie): Print ( ' % s% s watching movies ' % (name, movie)) SLEEP ( . 3 ) Print ( ' of The End ' ) # @ceal_time DEF play_game (name, Game): Print ( ' % s% s game play ' % (name, Game)) SLEEP ( . 3 ) Print ( ' Game Over ' ) IF the __name__ = = ' __main__ ' : watch_movie (name = " John Doe " , Movie = " cat and mouse " ) play_game (name = ' John Doe ', Game = ' Warcraft ' )
3. Perform results