current recursion level

Cameron Laird claird at lairds.us
Sat Aug 5 20:56:06 EDT 2006


In article <1154662146.325383.241710 at h48g2000cwc.googlegroups.com>,
Simon Forman <rogue_pedro at yahoo.com> wrote:
>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?



More information about the Python-list mailing list