Can a Python method check if it has been called from within itself?

前端 未结 2 949
旧时难觅i
旧时难觅i 2020-12-05 20:28

Let\'s say I have a Python function f and fhelp. fhelp is designed to call itself recursively. f should not be called rec

2条回答
  •  萌比男神i
    2020-12-05 21:11

    Use the traceback module for this:

    >>> import traceback
    >>> def f(depth=0):
    ...     print depth, traceback.print_stack()
    ...     if depth < 2:
    ...         f(depth + 1)
    ...
    >>> f()
    0  File "", line 1, in 
      File "", line 2, in f
     None
    1  File "", line 1, in 
      File "", line 4, in f
      File "", line 2, in f
     None
    2  File "", line 1, in 
      File "", line 4, in f
      File "", line 4, in f
      File "", line 2, in f
     None
    

    So, if any entry in the stack indicates that the code was called from f, the call was (in)directly recursive. The traceback.extract_stack method gives you an easy access to this data. The if len(l[2] ... statement in the example below simply counts the number of exact matches of the name of the function. To make it even prettier (thanks to agf for the idea), you could make it into a decorator:

    >>> def norecurse(f):
    ...     def func(*args, **kwargs):
    ...         if len([l[2] for l in traceback.extract_stack() if l[2] == f.func_name]) > 0:
    ...             raise Exception, 'Recursed'
    ...         return f(*args, **kwargs)
    ...     return func
    ...
    >>> @norecurse
    ... def foo(depth=0):
    ...     print depth
    ...     foo(depth + 1)
    ...
    >>> foo()
    0
    Traceback (most recent call last):
      File "", line 1, in 
      File "", line 5, in func
      File "", line 4, in foo
      File "", line 5, in func
    Exception: Recursed
    

提交回复
热议问题