我试图x从另一个函数范围内更改全局值,如下面的代码所示,
x
x = 1 def add_one(x): x += 1
然后我按如下方式在Python的交互式终端上执行语句序列.
>>> x 1 >>> x += 1 >>> x 2 >>> add_one(x) >>> x 2
为什么x仍然是2而不是3?
python integer scope function increment
function ×1
increment ×1
integer ×1
python ×1
scope ×1