1

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_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 )

So I try to see if it works and then this happens;

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

Why is the return statement not returning anything while the found_functions do get printed?

1
  • The return statement is returning something… but that's only executed if the if is true. Otherwise, you're running the other code, which doesn't return anything. it does recursively call the function, but it doesn't do anything with the result of that recursive call. Usually, the last line in the recursive case is a return that returns the value of the recursive call, or an expression built around it. Commented Aug 28, 2013 at 19:20

2 Answers 2

2

Here:

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

should be

return find_and_remove_functions( string[ function_end + 1: ], found_functions )
Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

You need to explain why calling find_and_remove_functions returns nothing. After all, the base has a return in it. It's just that the recursive case doesn't return what it gets back from the base case. So, the OP needs to do what karthikr's answer suggests.
@abarnert sure, updated the answer. Completely agree that the OP should do what karthikr suggested. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.