current recursion level

D

David Bear

Is there an easy way to get the current level of recursion? I don't mean
sys.getrecursionlimit. I want to know during a live run of a script how
many times the functions has recursed -- curses, I don't know how to say it
better.
 
S

Simon Forman

David said:
Is there an easy way to get the current level of recursion? I don't mean
sys.getrecursionlimit. I want to know during a live run of a script how
many times the functions has recursed -- curses, I don't know how to say it
better.

This might help,

import sys

def getStackDepth():
'''Return the current call stack depth.'''
n = 1
while True:
try:
sys._getframe(n)
except ValueError:
return n - 1
n += 1


def f(n=3):
'''Demo getStackDepth()'''
print 'hi!', n
if n:
return f(n - 1)
else:
return getStackDepth()

This is an evil hack. Never use it.

If you really need to know how many times your function has recursed
pass a counter down the "stack" and increment it every time you
recursively call your function. This will also give you an accurate
count when calling a different function or functions that then
recursively call back into your function, but you'll have to pass your
counter around or *shudder* use a global variable.

def g(n=0):
'''n is the number of recursions...'''
if n < 4:
return g(n + 1)
else:
return n


Peace,
~Simon
 
C

Cameron Laird

.
.
.
import sys

def getStackDepth():
'''Return the current call stack depth.'''
n = 1
while True:
try:
sys._getframe(n)
except ValueError:
return n - 1
n += 1 .
.
.
This is an evil hack. Never use it.
.
.
.
*This* is evil? I regularly see worse things here in clp.

I'll elaborate slightly: even though I'm among those who regularly
emphasize that certain constructs have nearly no place in applica-
tion, as opposed to "system", programming, counting stack frames to
reach their end seems to me like a minor violation of coding hygiene.
Am I missing something in your suggestion?
 
S

Simon Forman

Cameron said:
.
.
.
*This* is evil? I regularly see worse things here in clp.

I'll elaborate slightly: even though I'm among those who regularly
emphasize that certain constructs have nearly no place in applica-
tion, as opposed to "system", programming, counting stack frames to
reach their end seems to me like a minor violation of coding hygiene.
Am I missing something in your suggestion?

No, not really. My "evil hack" comment was meant to point out that
even though the function would work, it wasn't great to use in
"production" code, and further, that although python easily permits
things like counting call stack depth, the better way to count the
level of recursion in a recursive function call was to, well, count it.
:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top