将全局变量分配给局部变量

时间:2017-01-05 18:07:46

标签: python function variables global local

当我将全局列表分配到本地列表并编辑此本地列表时,列表都会被编辑。是因为python中的列表是指针,类似于C中的数组吗?

>>> source = [(0, 5), (1, 5), (2, 5), (3, 5), (4, 5)]
>>> def s():
...     source_queue = source
...     while len(source_queue)>0:
...             f(source_queue)
...
>>> def f(source_queue):
...     (x,y) = source_queue.pop(0)
...     print (x,y) in source
...     print source,source_queue
...
>>> s()

输出:

False
[(1, 5), (2, 5), (3, 5), (4, 5)] [(1, 5), (2, 5), (3, 5), (4, 5)]
False
[(2, 5), (3, 5), (4, 5)] [(2, 5), (3, 5), (4, 5)]
False
[(3, 5), (4, 5)] [(3, 5), (4, 5)]
False
[(4, 5)] [(4, 5)]
False
[] []

0 个答案:

没有答案