Closures and Decorators

Concepts of closures and decorators:

  Closure: An inner function is defined in an outer function, the inner function uses the temporary variables of the outer function, and the return value of the outer function is a reference to the inner function, which is called a closure

  Decorator: A decorator is a closure

 

Examples of decorators:

def func1():
print("func1() called")

def decorator(f):
print("before f() called")
return f

f1 = decorator(func1) 
f1()
f2 = decorator(func1)
f2() #When
we replace

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324692419&siteId=291194637