473,320 Members | 1,794 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,320 software developers and data experts.

current recursion level

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.

--
David Bear
-- let me buy your intellectual property, I want to own your thoughts --
Aug 4 '06 #1
3 8644
David Bear wrote:
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.

--
David Bear
-- let me buy your intellectual property, I want to own your thoughts --
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

Aug 4 '06 #2
In article <11**********************@h48g2000cwc.googlegroups .com>,
Simon Forman <ro*********@yahoo.comwrote:
>David Bear wrote:
>Is there an easy way to get the current level of recursion? I don't mean
Aug 6 '06 #3
Cameron Laird wrote:
In article <11**********************@h48g2000cwc.googlegroups .com>,
Simon Forman <ro*********@yahoo.comwrote:
David Bear wrote:
Is there an easy way to get the current level of recursion? I don't mean
.
.
.
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?
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.
:-)

Aug 6 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Patrick Lioi | last post by:
def foo(): pass foo is a function foo is a callable object foo has method __call__ defined foo.__call__ is a function foo.__call__ is a callable object foo.__call__ has method __call__...
8
by: Jakle | last post by:
Hi all. Need alittle help here. This is an example from "How to Think Like a Computer Scientist: Learning with Python, Chapter 5". It's an open source ebook, so if you feel like it you can find it...
3
by: Thomas Philips | last post by:
I'm teaching myself Python and never having used recursion and generators, created the simple recursive program shown below and single stepped through it in IDLE with the debugger turned on to get...
11
by: Ken | last post by:
Hello, I have a recursive Sierpinski code here. The code is right and every line works fine by itself. I wish for all of them to call the function DrawSierpinski. But in this cae it only calls...
18
by: MTD | last post by:
Hello all, I've been messing about for fun creating a trial division factorizing function and I'm naturally interested in optimising it as much as possible. I've been told that iteration in...
13
by: robert | last post by:
My code does recursion loops through a couple of functions. Due to problematic I/O input this leads sometimes to "endless" recursions and after expensive I/O to the Python recursion exception. What...
6
by: Andre Kempe | last post by:
hej folks. i have a heap with fixed size and want to determine the depth of a element with given index at compile-time. therefore i wrote some templates. however, when i use template...
30
by: Jeff Bigham | last post by:
So, it appears that Javascript has a recursion limit of about 1000 levels on FF, maybe less/more on other browsers. Should such deep recursion then generally be avoided in Javascript?...
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.