There are a lot of articles on the scope of python on the Internet, so the article I will share with you today allows you to easily get the scope of Python by learning these five knowledge points, for more information, see. There are a lot of articles on the scope of python on the Internet, so the article I will share with you today allows you to easily get the scope of Python by learning these five knowledge points, for more information, see.
">
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. py
Lzl
9
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:
# Local 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
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?
# Local scope
Def func ():
Name = "lzl"
Func () # execute a 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
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 ~~
# Domestic version 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:
# Domestic version scope
Name = "lzl"
Def f1 ():
Print (name)
Def f2 ():
Name = "eric"
Return f1
Ret = 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.
# Domestic version 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])
#
#
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 questions
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.
The above is a detailed explanation of the five knowledge points in the scope of Python. For more information, see other related articles in the first PHP community!