Detailed introduction to python decorators

王林
Release: 2020-07-08 13:40:09
Original
2778 people have browsed it

Python decorator is a function used to expand the functionality of the original function. The purpose is to add new functions to the function without changing the original function name (or class name). A decorator is essentially a closure, and closure is the core of a decorator.

Detailed introduction to python decorators

Definition

(Recommended tutorial: python tutorial)

python decorators (fuctional decorators ) is a function used to expand the functionality of the original function. The purpose is to add new functions to the function without changing the original function name (or class name).

The special thing about this function is that its return value is also a function. This function is a function with the "original" function embedded in it.

Detailed explanation

In fact, the decorator It is a closure that takes a function as a parameter and returns an alternative version of the function. Closure is the core of the decorator.

Briefly explain the characteristics of closure:

A function returned by a function Object. When this function object is executed, it depends on the variable value inside the non-function. At this time, the actual content returned by the function is as follows:

1. Function object;

2. External variables that the function object needs to use Variables and variable values;

The above is a closure. A closure must be nested in a function and must return a function object that calls external variables to be a closure.

Example:

#encoding=utf-8
import time
def now():
    print "current time is %s" %time.strftime("%Y-%m-%d %H-%M-%S")
res=now
res()
Copy after login

Result:

Detailed introduction to python decorators

##Now if we want to add some other functions to the now() function, such as automatically printing some logs before and after calling the function, but We don’t want to modify the definition of the original now(), so our decorator comes in handy.

Essentially, decorator is a higher-order function that returns a function. So we need to define a function that can print logs The decorator

After Python2.4, it is supported to use the identifier @ to apply the decorator to the function. You only need to add @ and the name of the decorator before the definition of the function

Code:

#encoding=utf-8
import time
#定义装饰器
def log(func):
    def wrapper(*args,**kw):
        print "call func is %s" %func.__name__
        return func(*args,**kw)
    return wrapper

@log
def now():
    now = time.strftime("%Y-%m-%d %H-%M-%S")
    print "current time is %s" %now

now()
Copy after login
Result:

Detailed introduction to python decorators

The above is the detailed content of Detailed introduction to python decorators. 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