Python: list is not mutated after for loop

前端 未结 4 2209
忘了有多久
忘了有多久 2020-12-21 18:26
a =[1,2]
for entry in a:
    entry = entry + 1
print a  

Shouldn\'t the list be mutated to[2,3]? The result came out as [1,2]

4条回答
  •  失恋的感觉
    2020-12-21 18:41

    No, why should it?

    entry is just a name, which the for loop assigns to each integer in the list. If you subsequently reassign that name to point to a different integer, the list doesn't care.

提交回复
热议问题