First look
1. Unique nested functions in python
2. Nested scope and closure phenomenon
3. Modification of nonlocal keywords and embedded scope variables
In retrospect, in the previous section, we introduced the LEGB indexing mechanism of variables: for a variable, first find it locally (within the function); then find the local scope of the nested function, and then find the current global scope.
So far, we have yet to introduce a scope, that is, the nested scope, that is, E, which is the local scope of the nested function.
What is a nested function?
Python has a very interesting place, that is, def functions can be nested in another def function. When the outer function is called, the inner def statement that runs to only completes the definition of the inner function, and will not call the inner function unless it is explicitly called after the nested function.
x = 99 def f1(): x = 88 def f2(): print(x) f2() f1() 88
It can be seen that the nested variable x in f1 covers the global variable x=99, and then the local variable in f2 refers to x=88 according to the quotation rules.
Let's talk about a special feature of nested scope:
The local scope becomes invalid immediately after the function ends, while the nested scope remains valid after the nested function returns.
def f1(): x = 88 def f2(): print(x) return f2 action = f1() action() 88
This example is very important and interesting. The function f2 is defined in the function f1, f2 refers to the variable x in the nested scope of f1, and f1 returns the function f2 as the return object. The most notable thing is that we obtained the returned f2 through the variable action. Although the f1 function has exited at this time, f2 still remembers the variable name x in the nested scope of f1.
The above language phenomenon is called a closure: a function that can remember the value of a nested scope variable, even though the scope no longer exists.
An application here is a factory function. The factory function defines an external function. This function simply generates and returns an embedded function, but returns but does not call it. Therefore, by calling this factory function, you can get the embedded function. A reference, embedded function is created by running the internal def statement when calling the factory function.
def maker(n): k = 8 def action(x): return x ** n + k return action f = maker(2) print(f) <function maker.<locals>.action at 0x00000000021C51E0>
Look at another example:
def maker(n): k = 8 def action(x): return x ** n + k return action f = maker(2) print(f(4)) 24
Here we can see that the embedded function action remembers two nested variables in the nested scope, one is variable k and the other is parameter n, even if the maker returns and exits later. We get the reference of the embedded function action by calling the external function maker. This method of function nesting will be frequently used in the decorators to be introduced later. This kind of nested scope reference is the main way that python functions can retain state information.
Let me talk about another keyword nonlocal
Local functions use global declarations to modify global variables by reference, and correspondingly, if you want to modify the variables in the nested scope within the embedded function, you must use nonlocal to declare.
def test(num): in_num = num def nested(label): nonlocal in_num in_num += 1 print(label, in_num) return nested F = test(0) F('a') F('b') F('c') a 1 b 2 c 3
Here we can see several points. We refer to the variable in_num in the embedded scope through the nonlocal keyword in the nested function, then we can modify it in the nested function, even if the test function has exited the call, this "memory" "Still valid.
One last example:
def test(num): in_num = num def nested(label): nonlocal in_num in_num += 1 print(label, in_num) return nested F = test(0) F('a') F('b') F('c') G = test(100) G('mm') a 1 b 2 c 3 mm 101
The embedded variable in_num of different embedded function copies F and G returned by multiple calls to the factory function are independent of each other.
❈
Author : soy sauce brother, Tsinghua program ape, IT mainstream
Column address: https://zhuanlan.zhihu.com/c_147297848
❈
Recent Popular Articles
Analyze Apple's stock price data with Python
Nginx+uwsgi deploy Django application
Analysis of Nearly 50,000 Poems of the Tang Dynasty by Text Mining
Python natural language processing analysis Yitian slaying the dragon
Python 3.6 realizes the crawling of text, pictures and hot comments of single bloggers' Weibo