1, block-level scope
Would you like to run the following program at this time with output? Will execution succeed?
#块级作用域
If 1 = 1:
name = "Lzl"
print (name) for
I in Range (a): 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
Code execution succeeded, no problem; in java/c#, execution of the above code prompts Name,age to be undefined and succeeds in Python, 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 what we've learned before, when we learn a function, a function is a separate scope, there is no block-level scope in Python, but there is a local scope; look at the following code
#局部作用域
def func ():
name = "Lzl"
print (name)
Run this code, think about whether there is output?
Traceback (most recent call last):
File "c:/users/l/pycharmprojects/s14/preview/day8/scope/main.py", line < module>
Print (name)
nameerror:name ' name ' is not defined
Run the error, I believe that everyone can understand that the name variable only in the func()
function within the effective, so in the global is not able to invoke the code to make a simple adjustment, and then see the results?
#局部作用域
def func ():
name = "Lzl"
func () #执行函数
print (name)
Add a code to the previous code, before the variable name printing, perform a function, at this time printing will change?
Traceback (most recent call last):
File "c:/users/l/pycharmprojects/s14/preview/day8/scope/main.py", line, in <module>
Print (name)
nameerror:name ' name ' is not defined
Execution is still an error, or back to the sentence: even if the function, the scope of name is only within the function, the external still can not make calls; remember the first two points of knowledge, and then start zooming in.
3. Scope Chain
Make adjustments to the function to see how the following code executes?
#作用域链
name = "Lzl"
def F1 ():
name = "Eric"
def f2 ():
name = "Snor"
print (name)
F2 ()
F1 ()
Learned the function, definitely know the final f1()
execution will output Snor
; We first remember a concept, Python has a scope chain, the variables will be from the inside to find, first to their scope to find, they did not go to the superior to find, until the error is not found
4, the ultimate version of the scope
Good, foreshadowing enough, the ultimate version of the coming ~ ~
#终极版作用域
name = "Lzl"
def F1 ():
print (name)
def f2 ():
name = "Eric"
F1 ()
F2 ()
Think of the final F2 () to print "Lzl" or "Eric"? Remember your answer, now do not post the answer, first look at the following code:
#终极版作用域
name = "Lzl"
def F1 ():
print (name)
def f2 ():
name = "Eric" return
f1
ret = F2 ()
ret ()
#输出: Lzl
The execution result is "Lzl", the above code is analyzed, and the f2()
result is the memory address of the function F1, that is, ret=f1
execution is equivalent to ret()
executing f1()
, executing f1()
with f2()
no relation, name=“lzl”
and f1()
in a chain of scopes, There is no variable inside the function that will look out, so the variable name value is "Lzl"; I understand that, then you know the answer to the final code that didn't give you an answer.
#终极版作用域
name = "Lzl"
def F1 ():
print (name)
def f2 ():
name = "Eric"
F1 ()
F2
() # Output: Lzl
Yes, the output is "Lzl", remember that before the function was executed, the scope was formed and the scope chain generated
5, Sina questions
Li = [lambda:x for x in range (10)]
Judge the type of Li? What are the types of elements inside Li?
Print (Type (LI))
print (Type (li[0]))
# <class ' list ' >
# <class ' function ' >
You can see the Li for list type, the element inside list is function, then print the return value of the first element in the list, at this time the return value is how much?
#lambada question
li = [lambda:x for x in range (Ten)]
res = li[0] ()
print (res)
#输出: 9
The return value of the first function of Li is 9 or not 0, remember: The function does not execute before the internal code is not executed; The code inside the blog can practice and deepen the impression
Summarize
The above is the entire content of this article, do not know to everyone's study and work can bring some help, if you have questions can message exchange.