[Python] function ⑥ ( variable scope | local variable | global variable | global keyword | code example )





1. Variable scope



Python variable scope is the scope of use of variables , in which code areas variables can be accessed, and in which code blocks they cannot be accessed;


Variables are mainly divided into two categories:

  • local variable
  • global variable

1. Local variables


Local variables refer to the variables defined in the function, which can only be accessed inside the function, and the local variables in the function cannot be accessed outside the function;

The role of local variables is to temporarily save the temporary variable data when the function is running, and the local variables will be destroyed when the function is finished running;


Example of wrong code : The local variable sum_num is defined in the add function in this code. This local variable can only be accessed inside the function. If it is accessed outside the function, an error will be reported at compile time and an error will be reported at runUnresolved reference 'sum_num' timeNameError: name 'sum_num' is not defined;

"""
局部变量 代码示例
"""

# 定义函数 以及 局部变量
def add(a, b):
    # sum_num 是局部变量, 只能在函数内部访问
    sum_num = a + b
    print(sum_num)

# 调用函数
add(1, 2) # 输出: 3

# 尝试调用 函数内部的 局部变量
print(sum_num) # 报错: NameError: name 'sum_num' is not defined

Results of the :

Y:\002_WorkSpace\PycharmProjects\pythonProject\venv\Scripts\python.exe Y:/002_WorkSpace/PycharmProjects/HelloPython/hello.py
3
Traceback (most recent call last):
  File "Y:\002_WorkSpace\PycharmProjects\HelloPython\hello.py", line 12, in <module>
    print(sum_num)
NameError: name 'sum_num' is not defined

Process finished with exit code 1

Error:

NameError: name 'sum_num' is not defined

insert image description here


2. Global variables


A global variable is a variable defined outside the function body, which can take effect both inside and outside the function body;

Use global variables outside the function body, just use it directly;

Global variables can be accessed inside the function body and can only be read, not modified;

If you want to use a global variable inside the function body, if you want to modify the value of the global variable, you need to use globalthe keyword to declare in advance, otherwise the variable will be regarded as a newly defined internal variable;


3. Code example - Failed to modify global variables in the function body


In the following code, the global variable sum_num is defined,

If you want to read the global variable in the add function, you can read it directly;

However, if you want to modify the global variable, you can directly use sum_num = a + bthe code to reassign the global variable. This operation is equivalent to redefining a new local variable in the function, which will be destroyed after the function is executed. The local variable sum_num and the global variable sum_num not related;

Therefore, the final printed value of the global variable sum_num is still 0;

Code example:

"""
全局变量 代码示例
"""

# 定义全局变量
sum_num = 0

# 定义函数 以及 尝试使用全局变量
def add(a, b):
    # 此处的 sum_num 是局部变量
    sum_num = a + b
    # 输出局部变量
    print(sum_num)

# 调用函数
add(1, 2) # 输出: 3

# 尝试调用 函数内部的 局部变量
print(sum_num) # 输出: 0

Results of the :

3
0

insert image description here


4. Code example - modifying global variables in the function body


In the following code, if you want to modify the global variable, you cannot use sum_num = a + bthe code , you need global sum_numto use the code first, and declare that the global variable sum_num will be used below, and the compiler will treat this variable as a global variable;

The final printed value of the global variable sum_num is 3;


Code example:

"""
全局变量 代码示例
"""

# 定义全局变量
sum_num = 0

# 定义函数 以及 尝试使用全局变量
def add(a, b):
    # 此处的 sum_num 是全局变量
    global sum_num
    sum_num = a + b
    # 输出全局遍变量
    print(sum_num)

# 调用函数
add(1, 2) # 输出: 3

# 输出全局变量
print(sum_num) # 输出: 3

Results of the :

3
3

insert image description here

Guess you like

Origin blog.csdn.net/han1202012/article/details/131015624