Five knowledge points of Python get scope done, and python knowledge points get done
1. Block-level scope
Think about the output of running the following program at this time? Will the execution be successful?
# Block-level scope if 1 = 1: name = "lzl" print (name) for I in range (10): age = iprint (age)
Let's take a look at the execution results first.
C:/Users/L/PycharmProjects/s14/preview/Day8/scope/main. pylzl9Process finished with exit code 0
The code is successfully executed and there is no problem. In Java/C #, the code above will prompt that name and age are not defined, but can be successfully executed in Python, this is because there is no block-level scope in Python. the variables in the code block can be called externally, so they can run successfully;
2. Local Scope
Looking back at the previous knowledge, when we were learning functions, the functions were a separate scope. Python had no block-level scope, but there was a local scope. Let's look at the following code:
# Partial scope def func (): name = "lzl" print (name)
Run this code and think about whether there will be output?
Traceback (most recent call last): File "C:/Users/L/PycharmProjects/s14/preview/Day8/scope/main. py ", line 23, in <module> print (name) NameError: name 'name' is not defined
I believe everyone can understand that the name variable takes effect only in the func () function, so it cannot be called globally. Make a simple adjustment to the above Code, what are the results?
# Partial scope def func (): name = "lzl" func () # Execute function print (name)
I added a code to the previous Code. before printing the variable name, I executed the function. Will the printing change?
Traceback (most recent call last): File "C:/Users/L/PycharmProjects/s14/preview/Day8/scope/main. py ", line 23, in <module> print (name) NameError: name 'name' is not defined
If the execution still reports an error, return to the previous sentence: even if the function is executed, the name scope is only within the function, and the call cannot be performed externally. Remember the first two knowledge points, next, we will start to zoom in.
3. Scope chain
Adjust the function to see how the following code is executed?
# Scope chain name = "lzl" def f1 (): name = "Eric" def f2 (): name = "Snor" print (name) f2 () f1 ()
After learning the function, you must know that the final f1 () execution will output Snor. Remember the concept that there is a scope chain in Python, and variables will be searched from inside to outside, go to your own scope to find it, and do not go to the upper-level to find it until the error is not found.
4. Release Scope
Okay, it's enough. The ultimate version is coming ~~
# Release scope name = "lzl" def f1 (): print (name) def f2 (): name = "eric" f1 () f2 ()
Think about whether the final f2 () execution result prints "lzl" or "eric "? Remember your answers. Don't post the answers now. Let's take a look at the following code:
# Release scope name = "lzl" def f1 (): print (name) def f2 (): name = "eric" return f1ret = f2 () ret () # output: lzl
The execution result is "lzl", and the above Code is analyzed. The f2 () execution result is the memory address of function f1, that is, ret = f1; the ret () is executed () it is equivalent to executing f1 (). When f1 () is executed, it has no relationship with f2 (). name = "lzl" and f1 () are in a scope chain, no variable is inside the function, so the variable name value is "lzl". After understanding this, you will know the final code that didn't give the answer just now.
# Release scope name = "lzl" def f1 (): print (name) def f2 (): name = "eric" f1 () f2 () # output: lzl
Yes, the output is "lzl". Remember that the scope has been formed before the function is executed, and the scope chain has also been generated.
5. Sina interview questions
li = [lambda :x for x in range(10)]
Determine the type of li? Why is the element type in li?
print(type(li))print(type(li[0]))# <class 'list'># <class 'function'>
We can see that li is a list type, and the elements in the list are functions. Then, the return value of the first element in the list is printed. What is the return value?
# Lambada interview question li = [lambda: x for x in range (10)] res = li [0] () print (res) # output: 9
The return value of the first function of li is 9 but not 0. Remember: the internal code is not executed before the function is executed. The code in the blog can be practiced by yourself to make a deeper impression.