a=1
def first():
global a
b=2
a=b
def second():
c=3
b=c
print(b)
second()
print(b)
first()
输出为 3 2
这里 first函数里 通过 global 引用全局变量a 并成功赋值,那second函数里怎么修改first里的变量b呢?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
可以看一下nonlocal.
https://docs.python.org/3/ref...
second的b是作用于second的局部变量和first里的b不是同一对象
你看你second里重新复制后最后print(b)不还是2么,没毛病。