Python scope, python
Before writing a scope, you must first understand the local variables and global variables. After understanding local variables and global variables, the concept of scope is well understood.
Local variable: Local variables can only be accessed locally. if they exceed the scope, they become ineffective.
Global Variables: Can be used in any scope. When the current scope has local variables with the same variable name, the local variables take effect.
(Forget to define it accurately, forget it)
1. None in Python
Block setScope Java/C # has a block set scope. Python/JavaScript has no block set scope. The so-called block set scope requires you to first understand the concept of Python code blocks. Python follows strict indentation standards and can be considered as a code block between the same indentation. Such as if judgment, for, while loop, etc.
If 1 = 1: # if, for and so on constitute a code block name = 'zhang' # print (name) after the code block ends)
In actual operation, print (name) has an output value, but it is in the same Indentation Position as if, that is, it belongs to different code blocks. Therefore,Python does not have block set scope.
2. In Python, the function is used as the scope.
Let's look at an example:
Def func (): # variables defined in the function can only be used in the function and cannot be referenced externally (in the function scope) name = 'zhang' print (name) >>> traceback (most recent call last): File "C:/Users/Administrator/PycharmProjects/s13/day10/scope. py ", line 11, in <module> print (name) NameError: name 'name' is not defined
When you run the above Code, an error is returned! In Python syntax, the function body has a separate scope. When running the Code, if the function does not execute the code in the function body by default, it will be executed only after the function is called. Therefore, the variables defined in the function can only be called in the function.
3. Python has a scope chain. If the current scope cannot be found, it will be searched up.
name = 'zhang'def func(): name='a' #print(name) def f2(): name='b' print(name) f2()func()>>>b
Variable name is defined in both func and f2. When executing func (), assign name = 'A' first, then execute f2 (), and redefine name = 'B ', when print (name) is executed, the output result is 'B'
Before the function is executed, the scope and scope chain are all determined.
Name = 'Alex 'def f1 (): print (name) # f1 inherits its own scope def f2 (): name = 'Eric' f1 () f2 () # Run f1 () >>> alex
name = 'zhang'def f1(): print(name)def f2(): name = 'eric' #print(name) return f1ret = f2()ret()>>>zhang
At this point, the scope of Python is basically completed. During the compilation of Python files, you need to pay attention to the scope of the variables to avoid referencing errors or failing to find the variables when using the variables.
Finally, let's analyze the final execution result:
li = [lambda :x for x in range(10)]r = li[0]()print(r)>>>9
# Li is a list
# The elements in li are lambda expressions and functions.
# Internal code is not executed before a function is called
# The output result of li [0] is the memory address of each function body, that is, li [0] Is the function.
# Li [0] () executes the Function
# Execute the for loop first. The result of the loop is 9, and then assign 9 to x.
# Because the for loop is executed 10 times, the length of all li resources is 10, and each element in it is 9. (X is out of the loop relative to the for loop. Therefore, x obtains the execution result of the for Loop instead of the 0-9 value)
Finally, we recommend Wu sir's "Five sentences to handle JavaScript scopes".