1. Closure
1. The concept of closures (Wikipedia)
In computer science, Closure is short for Lexical Closure, a function that references free variables. The referenced free variable will exist with the function, even if it has left the environment in which it was created. So, there is another way of saying that a closure is an entity composed of a function and its associated reference environment. Closures can have multiple instances at runtime, and different reference environments and the same function composition can produce different instances.
The above mentioned two key places: free variables and functions, these two keys will be discussed later. It is still necessary to repeat the meaning of "closure". If you understand the meaning of the text, you can visually understand it as a closed package. This package is a function. Of course, there is also the corresponding logic inside the function. The thing in the package is freedom. Variables, free variables can wander around with the package. Of course, there must be a premise that the package is created.
In Python, a closure is when you call a function A, which returns a function B to you. The returned function B is called a closure. The parameters you pass when calling function A are free variables.
Example one:
def line_config(content, lenth): def line(): print('-'*(lenth//2)+content+'-'*(lenth//2)) return line line1 = line_config( ' closure ' , 40 ) line1() line1()
G:\Super Brother\python3 programming\python3 basics\venv\Scripts\python.exe G:/Super brother/python3 programming/python3 basics/python functions.py
--------------- -----closure--------------------
--------------------closure- -------------------
Precautions:
1) In the closure, if you modify the referenced outer variable, you need to use the nonlocal variable declaration, otherwise it will be regarded as a newly defined variable in the closure
2) When a variable that will change later is referenced in the closure, be sure to pay attention
Example two:
def test(): num = 10 def test1(): num = 66 #Do not change the external variable value print (num) print (num) test1() print(num) return test1 result = test()
G:\Super Brother\python3 programming\python3 basics\venv\Scripts\python.exe G:/Super brother/python3 programming/python3 basics/python functions.py
10
66
10
If you want to change the external variable value, you need to add nonlocal
Example three:
def test():def test1(): nonlocal num = 66 print (num ) print (num) test1() print(num) return test1 result = test() G:\Super Brother\python3 programming\python3 basics\venv\Scripts\python.exe G: /Super brother/python3 programming/python3 basics/ python functions.py 10 66 66
Example three:
def test(): funcs = [] for i in range(1, 4): def test1(): print(i) funcs.append(test1) return funcs newFuncs = test() print(newFuncs) newFuncs [0] () newFuncs [ 1 ] () newFuncs [ 2 ] () G:\Super Brother\python3 programming\python3 basics\venv\Scripts\python.exe G: /Super brother/python3 programming/python3 basics/ python functions.py [<function test.<locals>.test1 at 0x000001F59E5E4C80>, <function test.<locals>.test1 at 0x000001F59E5E4D08>, <function test.<locals>.test1 at 0x000001F59E5E4BF8>] 3 3 3
Example four:
def test(): funcs = [] for i in range(1, 4): def test1(num): def inner(): print(num) return inner funcs.append(test1(i)) return funcs newFuncs = test() newFuncs [0] () newFuncs [ 1 ] () newFuncs [ 2 ] () G:\Super Brother\python3 programming\python3 basics\venv\Scripts\python.exe G: /Super brother/python3 programming/python3 basics/ python functions.py 1 2 3
2. Decorator
#Decorator def checklogin (func): def inner(): print ( ' login verification ' ) func() return inner @checklogin def fss(): print ( ' Say something ' ) @checklogin def ftp(): print ( ' send picture ' ) flag = 2 if flag == 1: fss() else: ftp() G:\Super Brother\python3 programming\python3 basics\venv\Scripts\python.exe G: /Super brother/python3 programming/python3 basics/ python functions.py Login authentication send pictures