With 5 knowledge points, you can easily get the scope of Python and easily get the scope of python.
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 = I print (age)
Let's take a look at the execution results first.
C:/Users/L/PycharmProjects/s14/preview/Day8/scope/main. pylzl9 Process 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 is onlyfunc()The function takes effect internally, so it cannot be called globally. Make a simple adjustment to the above Code and check the result?
# 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 the lastf1()Output after executionSnorWe should first remember a concept that Python has a scope chain, and variables will be searched from inside to outside. First, we should look for our own scope, and we will not go to our superiors until an error is reported.
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 f1 ret = f2 () ret () # output: lzl
The execution result is "lzl". Analyze the above Code,f2()The execution result is the memory address of function f1, that isret=f1; Executeret()Equivalent to executionf1() , Executef1()Time andf2()No relationship,name=“lzl”Andf1()In a scope chain, no variable in the function will be searched out, so the variable name value is "lzl"; Understand this, you 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.
Summary
The above is all about this article. I don't know if it will help you in your study and work. If you have any questions, please leave a message.