Self-referencing lists

前端 未结 3 1964
刺人心
刺人心 2020-12-11 23:39

Say you do the following:

a = [1]
a[0] = a

You end up with a as equal to [[...]]. What\'s going on here? How doe

3条回答
  •  庸人自扰
    2020-12-12 00:21

    We have a == [a] here, so in theory a should be printed as a list containing one element, which is a --- i.e. itself a list containing one element, which is itself a list containing one element, and so on. Or in print: an infinite number of [, followed by an infinite number of ]. The fact that we get instead [[...]] is just Python trying to be helpful and not actually printing an infinite number of [.

提交回复
热议问题