1. The concept of acting on
Variable scoping refers to the scope in which a variable takes effect, and there are two kinds of scoping in Python.
global scope
Global scopes are created during program execution and destroyed at the end of program execution. All areas outside functions are global scopes. Variables defined in a global scope are global variables, and global variables can be accessed anywhere in the program.
function scope
Function scopes are created at the time of a function call and destroyed at the end of the call. A new function scope is created for every call to the function (not created without a call). Variables defined in function scopes are local variables, which can only be accessed inside the function.
2、Local Variables
So-called local variables are variables defined inside the function body, i.e., they take effect only inside the function body.
def testA(): # Local variable a # When assigning a value to a variable in a function, the default is always to assign a value to a local variable # Local variables do not affect variables outside the function. a = 100 # Access inside the body of the function, with access to the a variable print(a) testA() # 100 print(a) # report an error:name 'a' is not defined
varianta
is defined in thetestA
Variables inside a function that are accessed outside the function immediately report an error.
The role of local variables: inside the body of the function, temporary storage of data, that is, when the function call is completed, then destroy the local variables.
3. Global variables
By global variables, we mean variables that take effect both inside and outside the function.
Think: If there is a piece of data in the functionA
sum function (math.)B
What should I do if I want to use all of them?
A: Store this data inside a global variable.
# Define global variable a a = 100 def testA(): print(a) # Access the global variable a and print the data stored in variable a def testB(): print(a) # Access the global variable a and print the data stored in variable a testA() # 100 testB() # 100
Reflections:testB
Function Requirements to Modify Variablesa
The value of the program is 200, how can I change the program?
a = 100 def testA(): print(a) def testB(): a = 200 print(a) testA() # 100 testB() # 200 print(f'global variablea = {a}') # global variablea = 100
Reflections: intestB
function inside thea = 200
variable in thea
is modifying the global variablea
What? - I don't know.
A: No. Observation of the above code reveals that line 15 getsa
is 100, still defining the global variablea
time value and does not return the value of the
testB
200 inside the function. synthesize:testB
function inside thea = 200
is defining a local variable.
(1) global keyword
Think: How can you modify a global variable inside a function body?
a = 100 def testA(): print(a) def testB(): # Want to change the value of the global variable a to be 200 # The global keyword declares that a is a global variable. global a a = 200 print(a) testA() # 100 testB() # 200 print(f'global variablea = {a}') # global variablea = 200
global
The function of the keyword is to declare a variable as a global variable inside a function. In other words if you wish to modify a global variable inside a function, you need to use theglobal
keyword to declare variables.
(2) Summary
If you assign the variable a=200 directly inside the function, at this point thea
Instead of modifying a global variable, it is equivalent to declaring a new local variable inside the function. To modify a global variable inside a function body: firstglobal
heralda
for a global variable, and then the variable is reassigned.
4. Finding of variables
When we use a variable, we preferentially look for it in the current scope and use it if there is a
If it doesn't then it continues to go to the previous level of scoping, and if it does then it is used, the
If it's still not there then it continues to go to the previous level of scoping, and so on.
Until the global scope is found and still not found, an exception is thrown NameError: name 'a' is not defined
。
# Exercise notes a = 10 def fn2(): def fn3(): a = 30 print('In fn3:','a =',a) fn3() print('In fn2:','a =',a) fn2() """ output result: fn3center: a = 30 fn2center: a = 10 """
5. Variable Data Type Variables in Scope
c = 10 def fn4(a): # Reassigning a formal parameter in a function doesn't affect other variables a = 20 print('a =', a, id(a)) fn4(c) print('c =', c, id(c)) """ output result: a = 20 8791349231264 c = 10 8791349230944 """ # If the data received by the formal parameter is a global list # When attempting to modify elements in a list within a function, the data in the global list is also changed c = [1,2,3] def fn4(a): # If the formal parameter executes an object, when we go through the formal parameter to modify the object # affects all variables that point to the object a[0] = 100 print('a =', a, id(a)) fn4(c) print('c =', c, id(c)) """ output result: a = [100, 2, 3] 5132808 c = [100, 2, 3] 5132808 """ # If we don't make a change to the global variable # # Then you need to use the shallow copying that we learned about earlier. # Or pass in a slice and it's solved # c = [1, 2, 3] def fn4(a): # Reassigning a formal parameter in a function doesn't affect other variables a[0] = 100 print('a =', a, id(a)) fn4(()) # fn4(c[:]) print('c =', c, id(c)) """ output result: a = [100, 2, 3] 6050824 c = [1, 2, 3] 6050312 """
6. Multi-function program execution flow
Generally in the actual development process, a program often consists of multiple functions, and multiple functions share certain data, as shown below:
(1) Shared global variables
# 1. Define global variables glo_num = 0 def test1(): global glo_num # Modify global variables glo_num = 100 def test2(): # Call the modified global variable in the test1 function print(glo_num) # 2. Call the test1 function and execute the function's internal code: declare and modify global variables. test1() # 3. Call the test2 function and execute the code inside the function: print test2() # 100
(2) Return value passed as a parameter
# First get the return value of function one, then pass this return value into function two def test1(): return 50 def test2(num): print(num) # 1. Save the return value of function test1. result = test1() # 2. Pass the variable with the function's return value as an argument to the test2 function. test2(result) # 50
summarize
This article on the Python variables in the scope of the article is introduced to this, more related Python variables in the scope of the content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!