Why does list(my_list) modify the object?

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

I happened on this peculiar behaviour accidentally:

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


        
2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-11 19:21

    The ... is only displayed when an item contains itself -- that is, the same object. list(a) makes a copy of the list, so the inner a isn't the same object. It only shows the ... when it gets to "a inside a", not "a inside list(a)".

提交回复
热议问题