Home  >  Article  >  Backend Development  >  A brief introduction to decorators in python

A brief introduction to decorators in python

零下一度
零下一度Original
2017-06-25 10:17:541117browse

Decorator Prelude 1

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

Decorator Prelude 2

<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>

Decorator Prelude 3

Decorator Prelude 4

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn