Strange interaction between timeit and recursion

Hrvoje Niksic hniksic at
Mon May 4 03:53:50 EDT 2009

Steven D'Aprano <steve at> writes:

> I don't understand why my recursive function hits the recursion
> limit inside the timeit.Timer when it works outside of it.

Probably because your test function is at the very edge of the
recursion limit, and timeit.Timer triggers it because it calls it at a
deeper stack level than is the case with the interactive prompt.
AFAIK the "recursion limit" simply limits the number of nested
function calls.

> Is there any way to see the current recursion depth at a particular
> moment?

I don't think that's possible from within Python, although maybe it
should be.  The current recursion depth is a simple member of the
thread state structure that could be exposed as a low-level function
that begins with an underscore, similar to sys._getframe.  The
"inspect" module and various debuggers might put it to good use.

More information about the Python-list mailing list