【发布时间】:2019-12-19 11:09:13
【问题描述】:
我的意思是,对于一个整数:
>>> a = 2
>>> def b():
... a += 1
...
>>> b()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in b
UnboundLocalError: local variable 'a' referenced before assignment
while 对于列表(或者说对于列表元素):
>>> a = [0]
>>> def b():
... a[0] += 1
...
>>> b()
>>> a[0]
1
【问题讨论】:
-
他们有完全相同的限制,如果你做了类似的操作,你会看到:
a += [1]。