By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,538 Members | 875 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,538 IT Pros & Developers. It's quick & easy.

Recursive lists

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.