current recursion level

Simon Forman rogue_pedro at yahoo.com
Sun Aug 6 15:51:23 EDT 2006


Cameron Laird wrote:
> 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?

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.
:-)




More information about the Python-list mailing list