Python 关键字 Nonlocal 与 Global

global 的用法:

1
2
3
4
5
6
7
8
9
10
11
12
num1 = 0
num2 = 0

def change_num():
global num1
num1 = 1
num2 = 1

change_num()
print(num1, num2)
# output: 1, 0
# 不允许直接修改全局变量(数组、字典除外),需要添加 global 关键字

nonlocal 的用法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
num1 = 0
num2 = 0

def change_num():
num3 = 1
num4 = 1

def change_num_inner():
nonlocal num3
num3 = 2
num4 = 2

change_num_inner()

return num3, num4

num1, num2 = change_num()
print(num1, num2)
# output: 2, 1

Python 关键字 Nonlocal 与 Global
https://wonderhoi.com/2023/08/07/Python-关键字-Nonlocal-与-Global/
作者
wonderhoi
发布于
2023年8月7日
许可协议