【问题标题】:Finding the level of recursion call in python在python中查找递归调用的级别
【发布时间】:2012-09-06 03:03:49
【问题描述】:

我有一个递归调用的函数,我想知道当前的递归级别。下面的代码显示了我用来计算它的方法,但它没有给出预期的结果。

例如: 查找系统路径的递归级别:

    import os
    funccount = 0

    def reccount(src):
        global funccount
        print "Function level of %s is %d" %(src, funccount)

    def runrec(src):
        global funccount
        funccount = funccount + 1
        lists = os.listdir(src)
        if((len(lists) == 0)):
            funccount = funccount - 1
        reccount(src)
        for x in lists:
             srcname = os.path.join(src, x)
             if((len(lists) - 1) == lists.index(x)):
                  if (not(os.path.isdir(srcname))):
                       funccount = funccount - 1
             if os.path.isdir(srcname):
                runrec(srcname)

    runrec(C:\test)

问题:给定目录路径,打印目录的递归级别

目录结构是: 在我的目录结构中,我将调用函数“reccount(Test)”(函数将使用 MainFolder 的路径调用)。我想知道每个文件夹的递归调用级别。 (仅限目录)

Test:
   |----------doc
   |----------share
                |----------doc
                            |----------file1
   |----------bin
                |----------common
                             |----------doc
   |----------extras
   |----------file2

当我调用该过程时,我得到以下结果:

    Function level of C:\test is 1
    Function level of C:\test\bin is 2
    Function level of C:\test\bin\common is 3
    Function level of C:\test\bin\common\doc is 3
    Function level of C:\test\doc is 3
    Function level of C:\test\extras is 3
    Function level of C:\test\share is 4
    Function level of C:\test\share\doc is 5

如你所见,当它打印 bin/common/doc 的结果时,它打印的是 3 而不是 4,并且所有后续结果都是错误的

【问题讨论】:

  • 我认为标题应该是“指定函数中的递归级别”。我发现这是在寻找“找到函数的递归级别”(不修改函数)。类似于堆栈跟踪的深度,以便检测深度调用。
  • 最直接/最省力的解决方案是stackoverflow.com/questions/34115298/… — 在import inspect 之后使用len(inspect.stack(0))。 (关于这个问题还有另一个更快的答案,但如果速度是一个问题,那么手动传递深度应该会更快。)

标签: python


【解决方案1】:
def some_method(data, level=0):


    some_method(..., level=level+1)


if __name__ == '__main__':
    some_method(my_data)

【讨论】:

    【解决方案2】:
    from inspect import getouterframes, currentframe
    import os
    
    def runrec(src):
        level = len(getouterframes(currentframe(1)))
        print("Function level of {} is {}".format(src, level))
        for x in os.listdir(src):
            srcname = os.path.join(src, x)
            if os.path.isdir(srcname):
                runrec(srcname)
    
    runrec('C:\\test')
    

    Function level of C:\test is 1
    Function level of C:\test\bin is 2
    Function level of C:\test\bin\common is 3
    Function level of C:\test\bin\common\doc is 4
    Function level of C:\test\doc is 2
    Function level of C:\test\extras is 2
    Function level of C:\test\share is 2
    Function level of C:\test\share\doc is 3
    

    【讨论】:

    • 虽然 Andreas 的答案是 OP 的明显选择,但对于不需要额外参数的解决方案 +1。对于从递归函数打印嵌套调试信息非常有用,无需额外参数的麻烦。
    • 如果你从另一个函数调用这个函数,数字会增加,而递归深度会保持不变,而基于参数的方法没有这个缺陷。
    • 只是一个问题.. getouterframes 和 currenteframe 的使用与使用 inspect.stack() 有何不同?
    【解决方案3】:

    为什么不将递归级别存储在参数中?

    def runrec(src, level=1):
      # ...
      runrec(new_src, level + 1)
    

    这样,你就不需要全局变量了:

    def reccount(src, level):
        print "Function count of {} is {}".format(src, level)
    

    【讨论】:

      猜你喜欢
      • 2015-04-23
      • 1970-01-01
      • 2017-08-02
      • 2013-09-15
      • 1970-01-01
      • 2014-02-04
      • 2012-11-05
      • 1970-01-01
      • 2017-06-11
      相关资源
      最近更新 更多