A brief introduction to decorators in python

零下一度
Release: 2017-06-25 10:17:54
Original
1160 people have browsed it

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>
Copy after login
Copy after login
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()
Copy after login
<br>
Copy after login
Copy after login

Implement the decorator Just reserve:

1. Functions are "variables"

2. Higher-order functions

3. Nested functions

High-order functions + nesting Function=》Decorator

 <br>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template