Home >Backend Development >Python Tutorial >A brief introduction to decorators in python
Definition: Essentially a function, used to decorate other functions, which is to add additional functions to other functions
Principles: 1. No Modify the source code and calling method of the modified function
<br>
import timedef timer(func):def warpper(*args,**kwargs): start_time = time.time() func() stop_time = time.time()print("the func run time is %s" % (stop_time-start_time))return warpper @timer #timer(test1)def test1(): time.sleep(3)print("in the test1") test1()
<br>
Implement the decorator Just reserve:
1. Functions are "variables"
2. Higher-order functions
3. Nested functions
High-order functions + nesting Function=》Decorator
<br>
The above is the detailed content of A brief introduction to decorators in python. For more information, please follow other related articles on the PHP Chinese website!