Walking deeply nested lists

Peter Otten __peter__ at web.de
Sat Aug 28 02:43:07 EDT 2010


donn wrote:

> * If an Instance calls a method on another Instance of the same
> class, is this still recursion? And how does this 'stack up'? I mean,
> literally, on the stack. Does each instance get its own stack, or does
> all the push, call, pop stuff happen in one main stack?
> (I worry about recursion depth limits because svg trees can get quite
> deep.)

If you call functions within functions (or methods, it doesn't matter) they 
consume stack space, e. g: 

>>> def alpha():
...     return beta()
...
>>> def beta():
...     return gamma()
...
>>> import random
>>> def gamma():
...     return random.choice([alpha, beta, gamma])()
...
>>> import sys
>>> sys.setrecursionlimit(10)
>>> alpha()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in alpha
  File "<stdin>", line 2, in beta
  File "<stdin>", line 2, in gamma
  File "<stdin>", line 2, in gamma
  File "<stdin>", line 2, in alpha
  File "<stdin>", line 2, in beta
  File "<stdin>", line 2, in gamma
  File "<stdin>", line 2, in beta
  File "<stdin>", line 2, in gamma
RuntimeError: maximum recursion depth exceeded

The normal recursion limit is 1000, I'm reducing it to give you a smaller 
traceback.

Peter



More information about the Python-list mailing list