python - return 语句在 python 递归中不返回任何内容

标签 python python-2.7 recursion return

下面的方法在字符串中查找是否有任何 python 方法。

def there_is_a_call( string ): 
    return string.find('(') > -1

def find_and_remove_functions( string , found_functions ): 
    if not there_is_a_call( string ):
        print( found_functions )
        return found_functions
    else: 
        function_end    = string.find('(')
        function_string = string[:function_end][::-1]
        if function_string.find('.') > -1 : 
            index = function_string.find('.')
        elif function_string.find(' ') > -1: 
            index = function_string.find(' ')
        else:
            index = len(function_string) - 1 
        func_name       = function_string[ : index + 1 ][::-1] + '()'
        new_list = found_functions 
        new_list.append( func_name )
        find_and_remove_functions( string[ function_end + 1: ], found_functions )

所以我尝试看看它是否有效,然后就会发生这种情况;

>>>> a = find_and_remove_functions( 'func() and some more()' , [] )
['func()', ' more()']
>>>> print(a)
None 

为什么 return 语句不返回任何内容,而 found_functions 却被打印出来?

最佳答案

这里:

find_and_remove_functions( string[ function_end + 1: ], found_functions )

应该是

return find_and_remove_functions( string[ function_end + 1: ], found_functions )

关于python - return 语句在 python 递归中不返回任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18496177/

相关文章:

algorithm - 如何得到一个粒度越来越细的序列 : 1, 16, 8, 4, 12, ...?

c - C 中链表的递归搜索函数

python - 何时在 Tensorflow 模型保存中使用 .ckpt、.hdf5 和 .pb 文件扩展名?

python - 无法嵌入 python 以使用压缩库

python - 在 Python 中实现蒙特卡罗马尔可夫链时出现错误

haskell - 递归类型是haskell,它不重复相同的构造函数

python - 如何重新实现QTextDocument createObject?

python - 我的 Instagram 机器人不喜欢任何帖子并以 KeyError : 0 结束 session

python - 将 Pandas 数据帧写入带有前导空格的文件

makefile - Python 构建进入无限循环