Does [:] slice only make shallow copy of a list?

前端 未结 5 1332
Happy的楠姐
Happy的楠姐 2020-12-10 08:41

I have experienced peculiar bugs from this [:] copy.

The docs say [:] makes only a shallow copy but seems:

a = [1,2,3]
id(         


        
5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-10 09:29

    The difference is that the elements of list a and list b are the same. Changing mutable objects in either list also effects the elements in the other list.

    In contrast there is deepcopy which tries to create a completely different set of objects.

提交回复
热议问题