python decorator (a)

First, let's look at the basic application form decorators:

def decorator(func):
    def inner(*args,**kwargs):
        data = func(*args,**kwargs)
        return data
    return inner

 

The outer function in a nested inner function, and the address of the function we need to perform, and passed to the outer function, the final function returns an internal memory address (inner).

application:

def decorator(func):
    def inner(*args,**kwargs):
        print('在函数执行前')
        data = func(*args,**kwargs)
        return data
    return inner


@decorator
def fu():
    return 'hello world'

data = fu()
print(data)

@decorator this role: equivalent to fu = decorator (fu) # We fu reassigned to return to the inner address fu is not performed at this time inner, inner fu address equal to address and report original fu address assignment. He gave func.

When we make the call fu () function, and so was the implementation of the inner function, the inner function executes the function fu in the outer function, we have already fu address assigned to the function func. Before executing fu () function and you can do some other operations, such as printing.

From the above results, it can be decorated on the basis that we do not modify the original function on, make some modifications of secondary functions. In an original function to another function package (similar closures).

 

II. Decorator with parameters

The basic format:

def decorator(n):
    def x(func):
        def inner(*args,**kwagrs):
            val = []
            for i in range(n):
                data = func()
                val.append(data)
            return val
        return inner
    return x



@decorator(9)
def fu():
    return 666


data = fu()
print(data)

 Decorative multi-parameter is based on the basic decorator on another layer of nested, its workflow:

1. The function performed first outermost layer, decorator (n), establishing in memory a function of x, and returns the address of the memory function of x, n = as a parameter, the value of accepted

2. then perform a function of x, the function established in the inner memory, and as a function of the parameter to be packaged, can penetrate into the inner, equivalent to fu = x (fu), and returns the memory inner address, so the decorative of which there are two values ​​is n = 9 func = fu

3. FU performed next (), equivalent to the function performed in the inner code, we can use the saved values ​​of the two following operations.

The end result is: [666, 666, 666, 666, 666, 666, 666, 666, 666]

 

Note: If without the use of our @ decorator assignment process is as follows:

def decorator(n):
    def x(func):
        def inner(*args,**kwagrs):
            val = []
            for i in range(n):
                data = func()
                val.append(data)
            return val
        return inner
    return x



def fu():
    return 666
####work process####
res = decorator(9)
res1 = res(fu)
fu = res1
data = fu()
print(data)

The results keep up with a result is the same, if not @, we use the following approach will also achieve the desired effect.

 

Guess you like

Origin www.cnblogs.com/jinyan-huang/p/11416522.html