user2246905
user2246905

Reputation: 1039

return inside an except python

What is the problem when including a function inside an except? In my case I have the following function:

def inventedfunction(list1):
    print "initial list %r" %list1

    SOMETHING THAT CREATES list2 based on list1

    try: 
        list2[1]
        print "inside try %r" %list2
        inventedfunction(list2) 
   except:
       print "inside except %r" %list2  
       return list2

After running inventedfunction(somelist) it seems everything is working:

initial list [3, 562, 7, 2, 7, 2, 3, 62, 6]
inside try [[3, 562], [2, 7], [2, 7], [3, 62], [6]]
initial list [[3, 562], [2, 7], [2, 7], [3, 62], [6]]
inside try [[2, 3, 7, 562], [2, 3, 7, 62], [6]]
initial list [[2, 3, 7, 562], [2, 3, 7, 62], [6]]
inside try [[2, 2, 3, 3, 7, 7, 62, 562], [6]]
initial list [[2, 2, 3, 3, 7, 7, 62, 562], [6]]
inside except [[2, 2, 3, 3, 6, 7, 7, 62, 562]]

But it is not returning anything. If I include return list2 outside the except it returns [[3, 562], [2, 7], [2, 7], [3, 62], [6]] but not [[2, 2, 3, 3, 6, 7, 7, 62, 562]] which is what I want.

Also if I change the code for this:

if len(list2)!=1:
    inventedfunction(list2) 
else:
    return list2

There is no return in the function.

Another simple example that also doesn't return anything:

def inventedfunction(list1):
    list2=list1[:-1]
    if len(list2)!=1:
        inventedfunction(list2) 
    else:
        return list2

Upvotes: 0

Views: 6476

Answers (2)

Anand S Kumar
Anand S Kumar

Reputation: 90889

You are recursively calling the function - inventendfunction() - but never returning the result you get from the recursive call, hence it does not return anything back , you would need to return the result returned by the recursive call as well -

try: 
    list2[1]
    print "inside try %r" %list2
    return inventedfunction(list2) 

Also, it is bad to have a bare except , you should think about what exception can come when calling inventedfunction() and only except those exceptions.


Since in the comments you say -

I guess my problem doesn't have to do with the function but understanding how recursion works.

Lets take a simple example of function a() that does recursion and is similar to yours -

>>> def a(i):
...     if i == 1:
...             return "Hello"
...     else:
...             a(i+1)
...
>>> print(a(0))
None

As you can see above simple example returned 0 , why? Lets take this step by step -

main -> a(0)
  1. You call function a(0) , here i is 0 , so you go to else part , and call a(1) .

    main -> a(0) -> a(1)
    
  2. Now, you are in a again, with i as 1 , now you go to if part, and this returns "Hello" .

    main -> a(0)         #Returned back to a(0)
    
  3. Now after the return you do not directly return to main() where you called a(0) , no it returns to whereever the function a(1) was called, and that was from inside a(0) , so you return to a(0) , and the execution continues, but since a(0) does not return anything, you get the default return value in main , which is `None.


For your example again, you need to add return inventedfunction(list2) , for it to correctly return the results from recursive calls, otherwise the return value of the recursed calls are thrown away and not returned back. Example -

def inventedfunction(list1):
    list2=list1[:-1]
    if len(list2)!=1:
        return inventedfunction(list2) 
    else:
        return list2

Upvotes: 5

BrenBarn
BrenBarn

Reputation: 251363

Your function only has a return in the except clause, so it doesn't return anything if there is no exception. Perhaps you meant to do return inventedfunction(list2) inside the try.

Upvotes: 2

Related Questions