Original link: https://chegva.com/5500.html
◎Knowledge points
-
Overview and use of closures
◎Script practice
"""Closure""" """ If another function is defined inside a function (let’s call the outer function and the inner function the outer function and the inner function respectively), the inner function references the variables in the outer function, and the return of the outer function The value is an inner function, and thus, constitutes a closure. """ def outer(): a = 10 def inner(): print(a) return inner """ Normally, variables defined inside a function are no longer available after the function call ends. However, for the closure, after the outer function is called, the variables referenced by the inner function in the outer function are still available. Because the variables referenced by the inner function in the outer function will be bound to the special attribute __closure__ of the inner function. """ def do_sth(): temp = 8 print(temp) do_sth() # print(temp) outer_result = outer() outer_result() # 10 outer()() # 10 print(outer_result.__closure__) # (<cell at 0x106153fa0: int object at 0x105f45a50>,) print(outer_result.__closure__[0].cell_contents) # 10 """ By default, the object referenced by the variable in the outer function cannot be modified in the inner function (if the referenced object is of mutable type, can modify the content of the object) """ def outer2(): a = 10 def inner2(): # Redefine a variable a, mask the variable a in the outer function # a = 11 # Equivalent to a = a + 1 # Redefine a variable a, and shield the variable a in the external function # When calculating a + 1 on the right side of the equal sign, the newly defined variable a has not been assigned, so the program alarms # a += 1 pass return inner2 def outer3(): a = [3] def inner3(): # Redefine a variable a, mask the variable a in the outer function # a = [1] # The object referenced by the variable a is of variable type, and the content of the object can be modified a[0] = 8 print(a) return inner3 outer3()() # [8] """ If you want to modify the object referenced by the variable in the outer function in the inner function, you can use the keyword nonlocal to declare the variable in the inner function, indicating that the inner function does not redefine a new variable of the same name, but uses A variable of that name in the outer function. """ def outer4(): a = 10 def inner4(): nonlocal a # a = 11 a += 1 print(a) return inner4 outer4_result = outer4() outer4_result() #11 """ For the variable in the outer function referenced in the inner function, the modification of the variable after the inner function is called will still be valid when the inner function is called next time, because the variable will be bound to the special attribute __closure__ of the inner function middle. """ outer4_result() #12
◎Script address: https://github.com/anzhihe/learning/blob/master/python/practise/learn-python/python_advanced/closure.py
This article is reprinted from: https://chegva.com/5500.html
This site is for inclusion only, and the copyright belongs to the original author.