Python - Russell's Paradox (Lists, Not Set)
I know it is possible to create lists within lists. But how much, exactly I can fit into one. I tried this in the IPython console:
In [1]: Alist = [1]
In [2]: Alist.append(Alist)
In [3]: Alist
Out[3]: [1, [...]]
In [4]: Alist[1]
Out[4]: [1, [...]]
In [5]: Alist[1][1]
Out[5]: [1, [...]]
In [6]: Alist[1][1][1]
Out[6]: [1, [...]]
Now I can keep on forever trying to access Alist[1][1][1][1]...[1]
, but how is this possible? Also, why didn't my car run out of memory? I am using Python2.7 on Ubuntu 16.04 if that helps.
source to share
There is only one final size object here:
First you create a list object with one item [1]
, and then you create a reference to that object that you call Alist
. When you add Alist
to "istelf" you are really just adding a reference to your object to the object itself. You now have a list object with two elements: 1
and a link to itself. There is no endless memory here.
Try this to convince yourself:
Alist = [1]
Alist.append(Alist)
Alist[1][0] = 0
Alist[0]
source to share