The closure function and decorators

Day12 closure function and decorators

Closure function

def f1():

​ def f2():

print ( "f2 function")

​ return f2

f2 = f1()

Closure function:

The closure function to the closure function + variable internal closure function inside the package then both the functions together and then return out through the return value in the form of

Closures have to function at least in line with nested functions

Decorator

Decorator: Decorative (new adds an extra feature)

Decorator function is essentially a function of the increase to the function

Note the following two points to a function when the function add features to increase functionality: decorator:

  1. Function without changing the original source code
  2. It does not change the original function call
def decorators(func):

​       def wrapper(*args,**kwargs):

​        res = func(*args,**kwargs)

​        return res 

return wrapper

Layer decorator

  1. For decorative functions, it is essentially a function
  2. Without changing the source code
  3. It does not change the way calls

Decorative template

def decorator(func):
    def wrapper(*args,**kwargs):
        res = func (*args,**kwargs)
        return res
    return wrapper
@decorator()

Iterator introduced

Iterables: Contains __iter__ method is called iterables

Iterator: Contains __iter__ and __next__ method called iterator

Builder: function containing the yield keyword is called the generator

Guess you like

Origin www.cnblogs.com/kaizi111/p/11622426.html