471,313 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,313 software developers and data experts.

Recursive lists

Can someone tell me why python doesn't crash when I do the following:
>>a=[]
a.append(a)
print a
[[...]]
>>print a[0][0][0][0][0][0][0]
[[...]]

How does python handle this internally? Will it crash or use up lot's
of memory in similar but more complicated cases?

Jul 24 '07 #1
3 1228
mi*******@gmail.com wrote:
Can someone tell me why python doesn't crash when I do the following:
>>>a=[]
a.append(a)
print a
[[...]]
>>>print a[0][0][0][0][0][0][0]
[[...]]

How does python handle this internally? Will it crash or use up lot's
of memory in similar but more complicated cases?

No, it remembers internally which objects it has seen when converting
them with str or repr. The C api includes functions Py_ReprEnter() and
Py_ReprLeave() which the builtin objects call when they enter/leave
repr. If Py_ReprEnter() returns 0 the object will return its
representation, on subsequent calls Py_ReprEnter() returns 1 and the
object knows it is being recursed.

If you have your own recursive data structures then you may have to take
your own precautions against recursion. The simplest way to do this is
to make sure that the recursion always goes through a builtin object
such as a list or dictionary.
>>class C:
.... def __repr__(self):
.... return "<C %r>" % self.inner
.... inner = None
....
>>c = C()
c.inner = c
c
File "<stdin>", line 3, in __repr__
File "<stdin>", line 3, in __repr__
File "<stdin>", line 3, in __repr__
... and so on ...
File "<stdin>", line 3, in __repr__
File "<stdin>", line 3, in __repr__
File "<stdin>", line 3, in __repr__
RuntimeError: maximum recursion depth exceeded
>>c.inner = [c]
c
<C [<C [...]>]>

N.B. Don't use a tuple here as tuples don't check for recursion. That's
because tuples cannot be recursive except by containing another
recursive data structure such as a list, dict, or user-defined object.
Jul 24 '07 #2
On Tue, 24 Jul 2007 02:14:38 -0700, mizrandir wrote:
Can someone tell me why python doesn't crash when I do the following:
>>>a=[]
a.append(a)
print a
[[...]]
>>>print a[0][0][0][0][0][0][0]
[[...]]

How does python handle this internally? Will it crash or use up lot's of
memory in similar but more complicated cases?
It should be like you pointing your finger at yourself and yelling I
guess. Just a reference, no new object.
Jul 24 '07 #3
So if I understood correctly the recursive structure isn't a problem
for python because "a" contains a reference to "a", not "a" itself. On
the other hand, print works ok because it has a special trap to detect
recursive structures. I think I understand it now.

Thnks for your replies, miz.

Jul 25 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Jon Slaughter | last post: by
4 posts views Thread by Piotr Filip Mieszkowski | last post: by
4 posts views Thread by Nicolas Vigier | last post: by
7 posts views Thread by cesco | last post: by
41 posts views Thread by Harry | last post: by
1 post views Thread by rekkufa | last post: by
1 post views Thread by nembo kid | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.