473,224 Members | 1,604 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 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 1306
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jon Slaughter | last post by:
I've managed to put together a template class that basicaly creates a recursive tree that lets you easily specify the "base" class of that tree and and ending notes and lets you stop the recursive...
4
by: Piotr Filip Mieszkowski | last post by:
Hello, I like both C++ and Lisp and sometimes try to mix their ideas in the code I write. Recently I started to think about writing a pair class similar to the CONS in Lisp. (For those of you...
4
by: Nicolas Vigier | last post by:
Hello, I have in my python script a function that look like this : def my_function(arg1, arg2, opt1=0, opt2=1, opt3=42): if type(arg1) is ListType: for a in arg1: my_function(a, arg2,...
2
by: RML | last post by:
hi guys i have searched all day and cannot find the code i am looking for. i am new to c# so please bear with me ;) i want to write a console app that simly lists in the console window, all...
7
by: cesco | last post by:
Hi, I have a dictionary of lists of tuples like in the following example: dict = {1: , 2: , 3: ] In this case I have three lists inside the dict but this number is known only at runtime. I...
41
by: Harry | last post by:
Hi all, 1)I need your help to solve a problem. I have a function whose prototype is int reclen(char *) This function has to find the length of the string passed to it.But the conditions...
1
by: rekkufa | last post by:
I am currently building a system for serializing python objects to a readable file-format, as well as creating python objects by parsing the same format. It is more or less complete except for a...
1
by: nembo kid | last post by:
I need of your advice. A good link where I can learn linked lists with recursive method (reverse, merge, sort, insertion and so on); well commented and explained.
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.