getrecursiondepth
Manlio Perillo
NOmanlio_perilloSPAM at libero.it
Fri Oct 1 13:30:05 EDT 2004
On Thu, 30 Sep 2004 18:28:30 GMT, Andrew Dalke <adalke at mindspring.com>
wrote:
>Me:
>>>Try this as a alternate solution for your style
>>>of use. It's still not the right one because it
>>>doesn't handle reloads nor multiple functions
>>>created through exec's.
>
>Manlio Perillo wrote:
>> No, I don't want this. "Hello!" should be printed every times.
>
>I see. I was writing code to emulate C's "static" behaviour
>which is not what you wanted.
>
It's my fault.
It is long time I'm not using C++ that I forgot that static variable
function initialization is done once during program.
However firstlevel_call is what I want.
>
>> Here is a functions that returns True if the caller's 'recursion
>> depth' is 1.
>> Please check if this is correct.
>
>>>>>def firstlevel_call():
>>
>> return sys._getframe(1).f_code != sys._getframe(2).f_code
>
> > N.B.: sys._getframe(2) can raise an exception
>
>Looks like it should work, and be thread-safe. Again,
>I don't suggest using it. It's going to be slower than
>one where you use pass the stack depth in as a parameter.
>It's also going to depend on implmentation behaviour.
>Someday a Python system may not instantiate the stack
>information until it's requested, causing a big run-time
>hit. But that's only theoretical.
>
Ok. Thanks
>> Sorry, I don't understand.
>> Here is my_function:
>>
>>
>> def my_function(a, b, *args, **kwargs):
>> print 'a, b, args, kwargs:', a, b, args, kwargs
>
>That's not recursive, so I don't know how to
>interpret your question.
This is only the function signature!
The problem is that I can't do:
def my_function(a, b, *args, **kwargs, __max_depth = 4): ...
The only way is to do:
def my_function(a, b, max_depth = 4, *args, **kwargs): ...
But in this way max_depth is 'exposed' to public.
In the first example it is private.
>Let's suppose you're
>doing Ackermann's function
>
>def Ackermann(m, n):
> if m == 0:
> return n+1
> if n == 0:
> return Ackermann(m-1, 1)
> return Ackermann(m-1, Ackermann(m, n-1))
>
What's the purpose of such a function?
Thanks and regards Manlio Perillo
More information about the Python-list
mailing list