return statement doesnt return anything in python recursion

前端 未结 2 499
孤街浪徒
孤街浪徒 2021-01-23 22:58

The methods below look in a string to find if it has any python methods.

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

def find_and_rem         


        
相关标签:
2条回答
  • 2021-01-23 23:21

    Here:

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

    should be

    return find_and_remove_functions( string[ function_end + 1: ], found_functions )
    
    0 讨论(0)
  • 2021-01-23 23:24

    Some more explanation here.

    a = find_and_remove_functions( 'func() and some more()' , [] ) prints a list because there is a line print( found_functions ) being executed.

    a is assigned to the result of find_and_remove_functions and, since the function returns nothing after the set of recursive calls (see your else part doesn't have a return), it is assigned to None.

    Here's a simple example of what is happening:

    >>> def test():
    ...     print "test"
    ... 
    >>> a = test()
    test
    >>> print(a)
    None
    >>> a is None
    True
    
    0 讨论(0)
提交回复
热议问题