The scope of a variable refers to the area where a variable can be valid, because when we use a function, some variables are defined in the main program, and some are defined in the called function. When our main program uses the function When defining a variable, an exception occurs. Let's introduce local variables and global variables.
Local variables, as the name suggests, are variables that act in a local area. If a variable is defined in a function, it will only work in the function. If a variable inside the function is used outside the function, an exception will occur.
Look at the following example:
deftest():data='local variable'print('Output data inside the function:',data)test()print('Output data in the main program:',data)
The output is:
The data output inside the function is: local variable Traceback(mostrecentcalllast):FileC:/Users/Qingyan/PycharmProjects/untitled/venv/Include/ts.py,line6,in<module>print('Output data in the main program: ',data)NameError:name'data'isnotdefined
Since the variable data is defined inside the function, when we use the variable data in the main program, there will be a problem that the variable being accessed does not exist. Therefore, we should pay attention to the fact that the variables defined inside the function are local variables. Unless otherwise declared, they will not be accessed. Can be used outside functions.
We can also understand global variables from the literal meaning that they are variables that act globally. Some students may naturally think that global variables must be variables defined in the main program. In fact, global variables can also act in functions. , here we introduce two ways of using global variables:
The scope of the variables we define in the main program is global, and we can also use these variables directly in the defined functions. See the following example:
data='Global variable data'deftest():print('This is a global variable that acts in the function:',data)test()print('This is a global variable that acts outside the function:',data)
Output result:
This is the global variable that acts in the function: global variable data This is the global variable that acts outside the function: global variable data
This method is the global variable we usually use. The variables we define in the main program can be used directly inside the function.
The variables we define within the function can also be programmed as global variables. At this time we need to use the global keyword.
First, let's take a look at what happens when the names of global variables and local variables are the same. Take a look at the following example:
data='Here is the global variable data'print(data)deftest():data='Here is the local variable data'print(data)test()print('Check the global variable data again:',data)
The output is:
Here is the global variable data. Here is the local variable data. Check the global variable data again: Here is the global variable data.
From this example, we can see that when the names of global variables and local variables are the same, it does not affect the contents of the global variable, but if we want to modify the value of the global variable in the function, then we have to use the global keyword.
Look at the following example again:
data='Here is the global variable data'print(data)deftest():globaldatadata='Here is the local variable data'print(data)test()print('Check the global variable data again:',data)
The output is:
Here is the global variable data. Here is the local variable data. Check the global variable data again: Here is the local variable data.
Through the global keyword, the local declaration tells the function that global is a global variable. After modification, all variables in the global are modified. The global keyword can make a variable become a global variable.
When there is no variable declared globally, we can also use the global keyword locally to directly declare a variable as a global variable. Then the variables we define in the function can also be used in the main program. See the following example:
deftest():globaldatadata='This is the variable data created locally'print('This is the output in the function:',data)test()print('This is the output in the main program:',data)
The output is:
This is the output in the function: This is the variable data created locally This is the output in the main program: This is the variable data created locally
Through the above example, we can understand the role of the global keyword in controlling global variables. In addition, we should pay attention to trying to avoid the names of global variables and local variables being the same when writing programs. Although they act in different areas, they will affect the interaction between them. Code understanding. That’s it for this section. Let’s learn about anonymous functions in the next section.