The basic format function decorators, decorator with parameters, and a plurality of simultaneous decoration of a function decorator

Contents:
standard format 1. decorators, and methods of use
2. decorator with parameters
plurality 3. Decorative same function decorator

1. Standard format, and use the decorator
we use in the process of function sometimes after a well-defined function, also want to add some new features, without changing the original function, can increase before or after the original function new code.
Method: function definition process, and then define a new function, the function achieved by the method of nested parameter passing.
The basic syntax is as follows:

def wrapper(func()):
    def inner():
       func() #要装饰的函数
    return inner

When called, the use of syntactic sugar @wrapper, close to the decorative function of position, for example,

@wrapper
def test1():
    print("test")

If you need to pass parameters, in accordance with the following syntax.

def wrapper(func):
    def inner(*args,**kwargs):
#添加函数调用前代码
        ret=func(*args,**kwargs)
#添加函数调用后代码
        return ret
    return inner

2. arguments decorator
assume that such an application: We also use the decorator for more than one function, if for some reason you need to temporarily cancel decorator, if not many functions, can be directly commented on it, but if many, very troublesome, time parameters can be introduced into the decorator, initial parameter assignment True, by modifying the initial parameters, such decorative function to fail.
When we actually call the decorator, the above code using the syntactic sugar (@wrapper) can not pass parameters to a decorator, so still have to define one function to achieve, as follows:

fagle=True
def wrapper_outer(fagle):
    def wrapper(func):
        def inner(*args,**kwargs):
            if fagle:
                # 添加函数调用前代码
                ret=func(*args,**kwargs)
                # 添加函数调用后代码
                return ret
            else:
                ret = func(*args, **kwargs)
                return ret
        return inner
    return wrapper

When the actual call, syntactic sugar instead @wrapper_outer (fagle) to

3. Decorative plurality decorator same function
when more decorative while simultaneously a decorative function, will run in a nested fashion, such as the following code

def wrapper1(func):
    def inne1r(*args, **kwargs):
        print("装饰器1前代码")
        ret = func(*args, **kwargs)
        print("装饰器1后代码")
        return ret
    return inner1
def wrapper2(func):
    def inner2(*args, **kwargs):
        print("装饰器2前代码")
        ret = func(*args, **kwargs)
        print("装饰器2后代码")
        return ret
    return inner2
@wrapper2
@wrapper1
def test():
    print("test")
test()

And wrapper2 two definitions wrapper1 decorators, decorative test1 simultaneously, will enter the following nested result, the following results:

2 code before decorator
decorator 1 code before
test
decorator After 1 Code
decorator code after 2

For example, similar to the Russian doll to as a nested layers executed, transmission parameters are more difficult to understand and to be understood with reference to the following code comments.

def wrapper1(func): #  2.调用函数,传参func=test
    def inner1(*args, **kwargs):  #13.执行inner1()
        print("装饰器1前代码")   #14.打印第二行代码
        ret = func(*args, **kwargs)  #15.执行func()等效于执行test(),上面第二步已经传参
        print("装饰器1后代码") #17.执行完func(),打印第三行代码,inner1执行完成
        return ret
    return inner1 #3.返回inner的内存地址,

def wrapper2(func): #6.func=inner1传参
    def inner2(*args, **kwargs):  #10.执行inner2
        print("装饰器2前代码") # 11.打印第一行代码
        ret = func(*args, **kwargs)  #12.执行func()等效于执行inner1() ,因为上面第六步已经为func传参为inner1
        print("装饰器2后代码")  #18.inner1执行完成,继续执行inner2,打印第五行,执行完成
        return ret
    return inner2   # 7. 返回inner2
@wrapper2  # 8.等效于 test=wrapper2(test) 调用wrapper2后,返回inner2,test=wrapper2(test)=inner2
@wrapper1  # 1.最靠近函数,先执行,等效于 test=wrapper1(test) 4调用warpper1后返回inner,test=inner1
def test():
    print("test") #16.执行第三行打印
test() #9.此时执行test()等效于执行inner2()

Compare calling back and forth around, look carefully or can understand, really do not understand, remember the results just fine

Published 13 original articles · won praise 1 · views 196

Guess you like

Origin blog.csdn.net/aa12551827/article/details/104546375