Why does list(my_list) modify the object?

后端 未结 2 1563
轻奢々
轻奢々 2020-12-11 18:52

I happened on this peculiar behaviour accidentally:

>>> a = []
>>> a[:] = [\'potato\', a]
>>> print a
[\'potato\', [...]]
>>         


        
2条回答
  •  半阙折子戏
    2020-12-11 19:27

    list() makes a shallow copy. The outer list is no longer the same object as the list it contains. It is printed as you would expect.

提交回复
热议问题