How to use the scope of variables in Python

王林
Release: 2023-05-12 15:01:06
forward
1406 people have browsed it

Preface

The scope of a variable refers to the area where the program code can access the variable. If it exceeds this area, an error will occur when accessing it again. In programs, variables are generally divided into "global variables" and "local variables" based on their "valid scope".

1. Local variables

Local variables refer to variables defined and used inside a function. They are only valid inside the function. That is, the names inside the function will only be created when the function is run. All names will no longer exist before or after the function runs. Therefore, if a variable defined inside the function is used outside the function, a NameError exception will be thrown.

For example, define a function named function, define a variable poem (called a local variable) inside the function, assign a value to it, then output the variable, and finally output the poem variable again outside the function body , the code is as follows:

def function():
    poem = '面朝大海,春暖花开'
    print('局部变量poem = ', poem)  # 输出局部变量的值
function()  # 调用函数
print('局部变量poem = ', poem)  # 在函数体外输出局部变量的值
Copy after login

Running the above code will display the exception as shown in the figure below.

How to use the scope of variables in Python

2. Global variables

correspond to local variables. Global variables are variables that can act inside and outside the function. Global variables mainly have the following two situations:

(1) If a variable is defined outside the function, it can be accessed not only outside the function, but also within the function. Variables defined outside the function body are global variables.

For example, define a global variable poem, and then define a function to output the value of the global variable poem within the function. The code is as follows:

poem = '花有重开日,人无再少年'  # 全局变量
def function():
    print('函数体内:全局变量poem =', poem)  # 在函数体内输出全局变量的值
function()  # 调用函数
print('函数体外:全局变量poem =', poem)  # 在函数体外输出全局变量的值
Copy after login

Run the above code, the following content will be displayed :

Inside the function: Global variable poem = Flowers will bloom again, and people will never be young again
Outside the function: Global variable poem = Flowers will bloom again, and people will never be young again

Explanation:When the local variable and the global variable have the same name, assigning values ​​to the variables in the function body will not affect the variables outside the function body. That is to say, everyone beats his own gong and everyone beats his own drum.

For example, define a global variable a outside the function body and assign it a value of 100, and define a local variable a inside the function body and assign it a value 10. The code is as follows:

a = 100  # 定义一个全局变量
def function():
    a = 10  # 定义局部变量
    print(a)  # 输出局部变量的值
function()  # 调用函数
print(a)  # 输出全局变量的值
Copy after login

The execution result is as follows:

10100

(2) After it is defined in the function body and modified with the global keyword, the variable becomes a global variable . The variable can also be accessed outside the function body and can be modified inside the function body.

For example, define a variable in the function body and use the global keyword to modify it. The code is as follows:

poem = '天若有情天亦老,人间正道是沧桑'  # 全局变量
print('函数体外:poem =', poem)  # 在函数体外输出全局变量的值
def function():
    global poem  # 将poem声明为全局变量
    poem = '人生得意须尽欢,莫使金樽空对月'  # 全局变量
    print('函数体内:poem =', poem)  # 在函数体内输出全局变量的值
function()  # 调用函数
print('函数体外:poem =', poem)  # 在函数体外输出全局变量的值
Copy after login

Run the above code, the following content will be displayed:

Function in vitro: poem = If the sky is sentimental, the sky will also grow old, and the right path in the world is the vicissitudes of life
Function inside the body: poem = If you are proud of life, you must have all the joy, don’t let the golden cup empty to the moon
Function outside the body: poem = If you are proud of life, you must have all the joy, don’t let the gold cup empty to the moon

From above It can be seen from the results that the global variable modified by the global keyword can modify the value of the global variable inside the function body.

Note:Although Python allows global variables and local variables to have the same name, it is not recommended to do so during actual development because it can easily confuse the code and make it difficult to distinguish which ones. are global variables and which are local variables.

The above is the detailed content of How to use the scope of variables in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template