Day 12 the closure function and decorators

The closure function and decorators

Closure function

Closure is closed (internal function function), the package comprising (outside of the inner layer of the function package local scope, so that for him is not outside the scope of global scope, but rather a function of the outer wrap corresponding to the local scope)

Pass parameters to a function of two ways

  1. Use parameters
def func(x):
    print(x)
    
func(1)   # 1
  1. Packet functions (using function names can be used as an argument to a function and the function name can be used as another function return values)
def outter(x):
    def func():
        print(x)
    return func
    
f=outter(1)   #  调用outter的时候内部没有可执行的代码,返回一个函数名func,把这个函数名赋值给f,同时在这个outter函数对应的局部名称空间中有一个x=1
f()         #   调用f()实质上是在调用func(),程序调用func()函数,执行内部print命令,然后从func对应的局部名称空间找x,然后再从外层包裹的outter函数对应的局部名称空间查找x,找到x=1,打印

Application package closure function

Significance closure package: the returned function object, not just a function object, outside the scope function also wrapped in a layer, which makes the function call preferentially used wherever their function corresponding to the outer wrap scope

Decorator

Is a tool means, and the function of the program is a tool provided with one function, the decorator means is to add additional functionality to the object to be decorated, thus defining decorator is to define a function, but the function is the function other functions used to add extra functionality, decorator itself can be any callable object, the object to be decorated can be any callable objects.

Why use decorator

Decoration may not modify the source code for the object to be decorated, as well as new features to be added without modifying the object decorated case where the object is called decor

How to use the decorator

# deco就是装饰器
def deco(func):
    def wrapper(*args,**kwargs):
        res=func(*args,**kwargs)
        return res
    return wrapper

def sleep():
    print('zaoshanghao')

sleep=deco(sleep)
sleep()
  1. Call decorator and as a parameter passed to sleep
  2. Interior decorator only a defined function and return function, so the calling function will not be executed when the wrapper function returns only the name wrapper
  3. Using the concept of function object is assigned to the original decorative function name, equivalent to assign the name to the original wrapper function name
  4. The original name of the function + () implementation of the time equivalent to calling interior decorators wrapper function
  5. Variable elongate wrapper function parameters participant receives all incoming outer decorators, and assigned to the function of the inner layer, if the incoming original name of a function, the original function can be called directly, the innermost layer
  6. Innermost function is called directly rather than defined, so the code runs directly
  7. Within the wrapper function can be added on demand function, if the original function returns a value, then, wrapper function should return a value, so we can function assigned to the innermost res, in the wrapper returnres achieve the same effect and the original function
  8. If the original function requires mass participation, because the wrapper equivalent to the original function, so we just give wrapper function parameter passing on the line, and because of uncertainty need to pass specific arguments and decorator can be a generic template, so we use the variable length argument \ parameter args and * kwarg shaped as a parameter, and the innermost layer of the wrapper function func parameter to receive all incoming parameters

Decorator syntax sugar

In the decorative objects directly over a single write a single line @ decorator name, you can save a line of code

Original function name = decorator name (the original function name)

Guess you like

Origin www.cnblogs.com/masterjian924/p/10957084.html