|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
>>> list3 = []
>>> def test():
for i in range(10):
list3.append(i)
print(list3)
>>> test()
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list3
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
---------------------------------------------------------------------------------
>>> list4 = []
>>> def test():
for i in range(10):
list4.append(i)
print(list4)
list4 = []
print(list4)
>>> test()
Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
test()
File "<pyshell#26>", line 3, in test
list4.append(i)
UnboundLocalError: local variable 'list4' referenced before assignment
我现在是知道可以直接在函数中修改列表的,而且修改后不需要global也可以影响函数外的列表(例如list3),但是为什么list4想要清空列表却报错呢
你 del list4 的话会把整个列表删掉,
del list4[:] 只会把 list4 从开头到结尾的内容删掉
|
|